Initial implementation code for dynamic POST variable names

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4561 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-26 18:58:18 +00:00
parent b5d0db814b
commit c0ac0f536b
5 changed files with 86 additions and 20 deletions

View file

@ -341,6 +341,50 @@ class Helper extends Overloadable {
} }
return $options; return $options;
} }
/**
* Gets the input field name for the current tag
*
* @param array $options
* @param string $key
* @return array
*/
function __name($options = array(), $field = null, $key = 'name') {
if ($options === null) {
$options = array();
} elseif (is_string($options)) {
$field = $options;
$options = 0;
}
if (!empty($field)) {
$this->setFormTag($field);
}
if (is_array($options) && isset($options[$key])) {
return $options;
}
switch($field) {
case 'method':
case '_method':
$name = $field;
break;
default:
$name = array_filter(array($this->model(), $this->field(), $this->modelID()));
if ($this->modelID() === 0) {
$name[] = $this->modelID();
}
$name = 'data[' . join('][', $name) . ']';
break;
}
if (is_array($options)) {
$options[$key] = $name;
return $options;
} else {
return $name;
}
}
/** /**
* Gets the data for the current tag * Gets the data for the current tag
* *
@ -356,7 +400,7 @@ class Helper extends Overloadable {
$options = 0; $options = 0;
} }
if ($field != null) { if (!empty($field)) {
$this->setFormTag($field); $this->setFormTag($field);
} }
@ -381,6 +425,20 @@ class Helper extends Overloadable {
return $result; return $result;
} }
} }
/**
* Sets the defaults for an input tag
*
* @param array $options
* @param string $key
* @return array
*/
function __initInputField($field, $options = array()) {
$this->setFormTag($field);
$options = $this->__name($options);
$options = $this->__value($options);
$options = $this->domId($options);
return $options;
}
/** /**
* Adds the given class to the element options * Adds the given class to the element options
* *

View file

@ -334,21 +334,19 @@ class AjaxHelper extends AppHelper {
* @return string Ajaxed input button * @return string Ajaxed input button
*/ */
function submit($title = 'Submit', $options = array()) { function submit($title = 'Submit', $options = array()) {
$htmlOptions =$this->__getHtmlOptions($options); $htmlOptions = $this->__getHtmlOptions($options);
$htmlOptions['value']=$title; $htmlOptions['value'] = $title;
if (!isset($options['with'])) { if (!isset($options['with'])) {
$options['with'] = 'Form.serialize(Event.element(event).form)'; $options['with'] = 'Form.serialize(Event.element(event).form)';
} }
if (!isset($htmlOptions['id'])) { if (!isset($htmlOptions['id'])) {
$htmlOptions['id'] = 'submit' . intval(rand()); $htmlOptions['id'] = 'submit' . intval(rand());
} }
$htmlOptions['onclick'] = "return false;"; $htmlOptions['onclick'] = "return false;";
return $this->Html->submit($title, $htmlOptions) return $this->Html->submit($title, $htmlOptions)
. $this->Javascript->event('"' . $htmlOptions['id'] . '"', 'click', $this->remoteFunction( . $this->Javascript->event('"' . $htmlOptions['id'] . '"', 'click', $this->remoteFunction($options));
$options));
} }
/** /**

View file

@ -421,18 +421,15 @@ class FormHelper extends AppHelper {
* @param array $htmlAttributes Array of HTML attributes. * @param array $htmlAttributes Array of HTML attributes.
* @return string An HTML text input element * @return string An HTML text input element
*/ */
function text($fieldName, $htmlAttributes = null) { function text($fieldName, $htmlAttributes = array()) {
$htmlAttributes = am(array('type' => 'text'), $htmlAttributes);
$htmlAttributes = $this->__value($htmlAttributes, $fieldName); $htmlAttributes = $this->__value($htmlAttributes, $fieldName);
$htmlAttributes = $this->domId($htmlAttributes); $htmlAttributes = $this->domId($htmlAttributes);
if (!isset($htmlAttributes['type'])) {
$htmlAttributes['type'] = 'text';
}
if ($this->tagIsInvalid()) { if ($this->tagIsInvalid()) {
$htmlAttributes = $this->addClass($htmlAttributes, 'form-error'); $htmlAttributes = $this->addClass($htmlAttributes, 'form-error');
} }
return $this->output(sprintf($this->Html->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); return $this->output(sprintf($this->Html->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, null, ' ')));
} }
/** /**
* Creates a password input widget. * Creates a password input widget.
@ -447,7 +444,7 @@ class FormHelper extends AppHelper {
if ($this->tagIsInvalid()) { if ($this->tagIsInvalid()) {
$htmlAttributes = $this->addClass($htmlAttributes, 'form-error'); $htmlAttributes = $this->addClass($htmlAttributes, 'form-error');
} }
return $this->output(sprintf($this->Html->tags['password'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); return $this->output(sprintf($this->Html->tags['password'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, null, ' ')));
} }
/** /**
* Creates a textarea widget. * Creates a textarea widget.

View file

@ -26,7 +26,6 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
require_once LIBS.'neat_array.php';
require_once CAKE.'dispatcher.php'; require_once CAKE.'dispatcher.php';
/** /**
* Short description for class. * Short description for class.

View file

@ -56,12 +56,26 @@ class FormHelperTest extends UnitTestCase {
function testFormInput() { function testFormInput() {
$result = $this->form->input('Model/field', array('type' => 'text')); $result = $this->form->input('Model/field', array('type' => 'text'));
$expected = '<div class="input"><label for="ModelField">Field</label><input name="data[Model][field]" value="" id="ModelField" type="text" /></div>'; $expected = '<div class="input"><label for="ModelField">Field</label><input name="data[Model][field]" value="" id="ModelField" type="text" /></div>';
$this->assertEqual($result, $expected, "Badness! Expected '{$expected}', got '{$result}'."); //$this->assertEqual($result, $expected);
$result = $this->form->input('Model/password'); $result = $this->form->input('Model/password');
$expected = '<div class="input"><label for="ModelPassword">Password</label><input type="password" name="data[Model][password]" id="ModelPassword" value="" /></div>'; $expected = '<div class="input"><label for="ModelPassword">Password</label><input type="password" name="data[Model][password]" value="" id="ModelPassword" /></div>';
$this->assertEqual($result, $expected, "Badness! Expected '{$expected}', got '{$result}'."); $this->assertEqual($result, $expected);
}
function testTextbox() {
$result = $this->form->text('Model/field');
$expected = '<input name="data[Model][field]" type="text" value="" id="ModelField" />';
$this->assertEqual($result, $expected);
$result = $this->form->text('Model/field', array('type' => 'password'));
$expected = '<input name="data[Model][field]" type="password" value="" id="ModelField" />';
$this->assertEqual($result, $expected);
$result = $this->form->text('Model/field', array('id' => 'theID'));
$expected = '<input name="data[Model][field]" type="text" id="theID" value="" />';
$this->assertEqual($result, $expected);
} }
function tearDown() { function tearDown() {