Added FormHelper::inputDefaults setter/getter method

This commit is contained in:
Tigran Gabrielyan 2012-05-20 12:42:47 -07:00
parent 2ad406ab64
commit 1d77ad393c
2 changed files with 80 additions and 0 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

@ -2583,4 +2583,22 @@ 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 mixed array|void Current inputDefaults
*/
public function inputDefaults($defaults = null, $merge = false) {
if (is_null($defaults)) {
return $this->_inputDefaults;
}
if ($merge) {
$this->_inputDefaults = array_merge($this->_inputDefaults, (array)$defaults);
} else {
$this->_inputDefaults = (array)$defaults;
}
}
}