Implementing Form helper default options. Allows the creation of persistent form options when opening a form.

Adding patch from 'Ceeram' / 'bankai'
Refs #56.
This commit is contained in:
mark_story 2009-10-15 00:44:38 -04:00
parent 6dcc819121
commit f9f2986f48
2 changed files with 46 additions and 6 deletions

27
cake/libs/view/helpers/form.php Normal file → Executable file
View file

@ -72,6 +72,14 @@ class FormHelper extends AppHelper {
*/
var $requestType = null;
/**
* Persistent default options used by input(). Set by FormHelper::create().
*
* @var string
* @access protected
*/
var $_inputDefaults = array();
/**
* Returns an HTML FORM element.
*
@ -82,6 +90,7 @@ class FormHelper extends AppHelper {
* - 'url' The url the form submits to. Can be a string or a url array,
* - 'default' Allows for the creation of Ajax forms.
* - 'onsubmit' Used in conjunction with 'default' to create ajax forms.
* - 'inputDefaults' set the default $options for FormHelper::input()
*
* @access public
* @param string $model The model object which the form is being defined for
@ -173,8 +182,11 @@ class FormHelper extends AppHelper {
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
'action' => null,
'url' => null,
'default' => true),
'default' => true,
'inputDefaults' => array()),
$options);
$this->_inputDefaults = $options['inputDefaults'];
unset($options['inputDefaults']);
if (empty($options['url']) || is_array($options['url'])) {
if (empty($options['url']['controller'])) {
@ -601,8 +613,11 @@ class FormHelper extends AppHelper {
$this->setEntity($fieldName);
$entity = join('.', $view->entity());
$defaults = array('before' => null, 'between' => null, 'after' => null);
$options = array_merge($defaults, $options);
$options = array_merge(
array('before' => null, 'between' => null, 'after' => null),
$this->_inputDefaults,
$options
);
if (!isset($options['type'])) {
$options['type'] = 'text';
@ -778,10 +793,10 @@ class FormHelper extends AppHelper {
unset($options['dateFormat']);
}
$type = $options['type'];
$before = $options['before'];
$type = $options['type'];
$before = $options['before'];
$between = $options['between'];
$after = $options['after'];
$after = $options['after'];
unset($options['type'], $options['before'], $options['between'], $options['after']);
switch ($type) {

View file

@ -4711,6 +4711,31 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
}
/**
* test that inputDefaults are stored and used.
*
* @return void
**/
function testCreateWithInputDefaults() {
$this->Form->create('User', array(
'inputDefaults' => array('div' => false, 'label' => false)
));
$result = $this->Form->input('username');
$expected = array(
'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername', 'value' => '')
);
$this->assertTags($result, $expected);
$result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserUsername'), 'username', '/label',
'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername', 'value' => ''),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* Test base form url when url param is passed with multiple parameters (&)
*