From 7e9b05e429c0c15b22e07b6124028f13385fdfe5 Mon Sep 17 00:00:00 2001 From: nate Date: Sun, 28 Oct 2007 05:28:45 +0000 Subject: [PATCH] Adding hidden form inputs for REST routing git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5913 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/dispatcher.php | 8 +++++++ cake/libs/view/helper.php | 7 ++++-- cake/libs/view/helpers/ajax.php | 2 +- cake/libs/view/helpers/form.php | 16 ++++--------- .../cases/libs/view/helpers/form.test.php | 24 +++++++++++++++---- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 3006f9b74..231d8fa11 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -349,6 +349,14 @@ class Dispatcher extends Object { } else { $params['form'] = $_POST; } + if (isset($params['form']['_method'])) { + if (isset($_SERVER) && !empty($_SERVER)) { + $_SERVER['REQUEST_METHOD'] = $params['form']['_method']; + } else { + $_ENV['REQUEST_METHOD'] = $params['form']['_method']; + } + unset($params['form']['_method']); + } } if (isset($params['form']['data'])) { diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 3ac90f622..daf27b1e3 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -392,8 +392,11 @@ class Helper extends Overloadable { * @return mixed */ function domId($options = null, $id = 'id') { - if (is_array($options) && !isset($options[$id])) { + if (is_array($options) && !array_key_exists($id, $options)) { $options[$id] = $this->model() . Inflector::camelize($this->field()); + } elseif (is_array($options) && $options[$id] === null) { + unset($options[$id]); + return $options; } elseif (!is_array($options)) { $this->setFormTag($options); return $this->model() . Inflector::camelize($this->field()); @@ -419,7 +422,7 @@ class Helper extends Overloadable { $this->setFormTag($field); } - if (is_array($options) && isset($options[$key])) { + if (is_array($options) && array_key_exists($key, $options)) { return $options; } diff --git a/cake/libs/view/helpers/ajax.php b/cake/libs/view/helpers/ajax.php index f94976d31..6fc668a9f 100644 --- a/cake/libs/view/helpers/ajax.php +++ b/cake/libs/view/helpers/ajax.php @@ -552,7 +552,7 @@ class AjaxHelper extends AppHelper { * @return string JavaScript block to create a droppable element */ function dropRemote($id, $options = array(), $ajaxOptions = array()) { - $options['onDrop'] = "function(element, droppable) {" . $this->remoteFunction($ajaxOptions) . "}"; + $options['onDrop'] = "function(element, droppable, event) {" . $this->remoteFunction($ajaxOptions) . "}"; $options = $this->_optionsToString($options, array('accept', 'overlap', 'hoverclass')); $options = $this->_buildOptions($options, $this->dropOptions); return $this->Javascript->codeBlock("Droppables.add('{$id}', {$options});"); diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 01f47831c..42d1ef052 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -184,7 +184,7 @@ class FormHelper extends AppHelper { case 'post': case 'put': case 'delete': - //$append .= $this->hidden('_method', array('name' => '_method', 'value' => up($options['type']), 'id' => $options['id'] . 'Method')); + $append .= $this->hidden('_method', array('name' => '_method', 'value' => up($options['type']), 'id' => null)); default: $htmlAttributes['method'] = 'post'; break; @@ -709,12 +709,7 @@ class FormHelper extends AppHelper { $output = null; if (isset($object) && isset($options['value']) && ($options['value'] == 0 || $options['value'] == 1)) { $db =& ConnectionManager::getDataSource($object->useDbConfig); - if (is_object($db)) { - $value = $db->boolean($options['value'], false); - } else { - pr(get_class($object)); - pr($object->useDbConfig); - } + $value = $db->boolean($options['value'], false); $options['value'] = 1; } $output = $this->hidden($fieldName, array('value' => '0', 'id' => $options['id'] . '_'), true); @@ -856,11 +851,10 @@ class FormHelper extends AppHelper { if (!empty($options['value']) || $options['value'] === '0') { $value = $options['value']; } - $this->__secure($key, $value); - /*if (in_array($fieldName, array('_method', '_fields'))) { - $model = null; - }*/ + if (!in_array($fieldName, array('_method'))) { + $this->__secure($key, $value); + } return $this->output(sprintf($this->Html->tags['hidden'], $options['name'], $this->_parseAttributes($options, array('name', 'class'), '', ' '))); } /** diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index d13f0687d..6b79b9baa 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -44,6 +44,7 @@ class ContactTestController extends Controller { } class Contact extends CakeTestModel { + var $primaryKey = 'id'; var $useTable = false; var $name = 'Contact'; @@ -60,6 +61,7 @@ class Contact extends CakeTestModel { } class UserForm extends CakeTestModel { + var $useTable = false; var $primaryKey = 'id'; var $name = 'UserForm'; @@ -97,6 +99,7 @@ class OpenidUrl extends CakeTestModel { } class ValidateUser extends CakeTestModel { + var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateUser'; @@ -119,6 +122,7 @@ class ValidateUser extends CakeTestModel { } class ValidateProfile extends CakeTestModel { + var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateProfile'; @@ -144,6 +148,7 @@ class ValidateProfile extends CakeTestModel { } class ValidateItem extends CakeTestModel { + var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateItem'; @@ -303,7 +308,16 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($result); $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); - $this->assertPattern('/^]*>$/', $result); + $this->assertPattern('/^]+>]+\/>$/', $result); + $this->assertPattern('/^]+id="UserFormLoginForm"[^<>]*>/', $result); + $this->assertPattern('/^]+method="post"[^<>]*>/', $result); + $this->assertPattern('/^]+action="\/user_forms\/login\/"[^<>]*>/', $result); + $this->assertNoPattern('/]+[^id|method|action]=[^<>\/]*>/', $result); + + $this->assertPattern('/]+type="hidden"[^<>]*\/>/', $result); + $this->assertPattern('/]+name="_method"[^<>]*\/>/', $result); + $this->assertPattern('/]+value="POST"[^<>]*\/>/', $result); + $this->assertNoPattern('/]+[^type|name|value]=[^<>\/]*\/>/', $result); $expected = array('OpenidUrl' => array('openid_not_registered' => 1)); $this->assertEqual($this->Form->validationErrors, $expected); @@ -326,7 +340,7 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); - $this->assertPattern('/^]*>$/', $result); + $this->assertPattern('/^]*>]+\/>$/', $result); $expected = array( 'ValidateUser' => array('email' => 1), @@ -356,7 +370,7 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); - $this->assertPattern('/^]*>$/', $result); + $this->assertPattern('/^]*>]+\/>$/', $result); $expected = array( 'ValidateUser' => array('email' => 1), @@ -1079,7 +1093,7 @@ class FormHelperTest extends CakeTestCase { function testFormMagicInput() { $result = $this->Form->create('Contact'); - $this->assertPattern('/^$/', $result); + $this->assertPattern('/^]+\/>$/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $result = $this->Form->input('name'); @@ -1133,7 +1147,7 @@ class FormHelperTest extends CakeTestCase { function testFormMagicInputLabel() { $result = $this->Form->create('Contact'); - $this->assertPattern('/^$/', $result); + $this->assertPattern('/^]+\/>$/', $result); $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false)); $this->assertPattern('/^$/', $result);