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
This commit is contained in:
nate 2007-10-28 05:28:45 +00:00
parent fbd3811cd1
commit 7e9b05e429
5 changed files with 38 additions and 19 deletions

View file

@ -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'])) {

View file

@ -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;
}

View file

@ -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});");

View file

@ -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'), '', ' ')));
}
/**

View file

@ -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('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/user_forms\/login\/"[^>]*>$/', $result);
$this->assertPattern('/^<form\s+[^<>]+><input\s+[^<>]+\/>$/', $result);
$this->assertPattern('/^<form[^<>]+id="UserFormLoginForm"[^<>]*>/', $result);
$this->assertPattern('/^<form[^<>]+method="post"[^<>]*>/', $result);
$this->assertPattern('/^<form[^<>]+action="\/user_forms\/login\/"[^<>]*>/', $result);
$this->assertNoPattern('/<form[^<>]+[^id|method|action]=[^<>\/]*>/', $result);
$this->assertPattern('/<input[^<>]+type="hidden"[^<>]*\/>/', $result);
$this->assertPattern('/<input[^<>]+name="_method"[^<>]*\/>/', $result);
$this->assertPattern('/<input[^<>]+value="POST"[^<>]*\/>/', $result);
$this->assertNoPattern('/<input[^<>]+[^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('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*>$/', $result);
$this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><input\s+[^<>]+\/>$/', $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('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*>$/', $result);
$this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><input\s+[^<>]+\/>$/', $result);
$expected = array(
'ValidateUser' => array('email' => 1),
@ -1079,7 +1093,7 @@ class FormHelperTest extends CakeTestCase {
function testFormMagicInput() {
$result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result);
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><input\s+[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<form[^<>]+[^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('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result);
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><input\s+[^<>]+\/>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
$this->assertPattern('/^<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);