Merge pull request #659 from tigrang/inputDefaults-setter

Input defaults setter for FormHelper
This commit is contained in:
José Lorenzo Rodríguez 2012-05-20 16:40:20 -07:00
commit bb368c569c
2 changed files with 77 additions and 1 deletions

View file

@ -7939,4 +7939,66 @@ class FormHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
}
/**
* Test inputDefaults setter and getter
*
* @return void
*/
public function testInputDefaults() {
$this->Form->create('Contact');
$this->Form->inputDefaults(array(
'label' => false,
'div' => array(
'style' => 'color: #000;'
)
));
$result = $this->Form->input('Contact.field1');
$expected = array(
'div' => array('class' => 'input text', 'style' => 'color: #000;'),
'input' => array(
'type' => 'text', 'name' => 'data[Contact][field1]',
'id' => 'ContactField1'
),
'/div'
);
$this->assertTags($result, $expected);
$this->Form->inputDefaults(array(
'div' => false,
'label' => 'Label',
));
$result = $this->Form->input('Contact.field1');
$expected = array(
'label' => array('for' => 'ContactField1'),
'Label',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][field1]',
'id' => 'ContactField1'
),
);
$this->assertTags($result, $expected);
$this->Form->inputDefaults(array(
'label' => false,
), true);
$result = $this->Form->input('Contact.field1');
$expected = array(
'input' => array(
'type' => 'text', 'name' => 'data[Contact][field1]',
'id' => 'ContactField1'
),
);
$this->assertTags($result, $expected);
$result = $this->Form->inputDefaults();
$expected = array(
'div' => false,
'label' => false,
);
$this->assertEqual($result, $expected);
}
}

View file

@ -346,7 +346,7 @@ class FormHelper extends AppHelper {
'encoding' => strtolower(Configure::read('App.encoding')),
'inputDefaults' => array()),
$options);
$this->_inputDefaults = $options['inputDefaults'];
$this->inputDefaults($options['inputDefaults']);
unset($options['inputDefaults']);
if (!isset($options['id'])) {
@ -2583,4 +2583,18 @@ class FormHelper extends AppHelper {
return $result;
}
/**
* Set/Get inputDefaults for form elements
*
* @param array $defaults New default values
* @param boolean Merge with current defaults
* @return array inputDefaults
*/
public function inputDefaults($defaults = null, $merge = false) {
if (!is_null($defaults)) {
$this->_inputDefaults = array_merge($merge ? $this->_inputDefaults : array(), (array)$defaults);
}
return $this->_inputDefaults;
}
}