diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php old mode 100644 new mode 100755 index 4c1e831d9e9..8433a5a5be9 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -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 @@ function create($model = null, $options = array()) { '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 @@ function input($fieldName, $options = array()) { $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 @@ function input($fieldName, $options = array()) { 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) { diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 31596cae2f3..cabda21cb8f 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -4711,6 +4711,31 @@ function testFormCreate() { $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 (&) *