Permalink
Browse files

Implementing Form helper default options. Allows the creation of pers…

…istent form options when opening a form.

Adding patch from 'Ceeram' / 'bankai'
Refs #56.
  • Loading branch information...
1 parent 6dcc819 commit f9f2986f481d244257e8756997d4a6297bb3884f @markstory markstory committed Oct 15, 2009
Showing with 46 additions and 6 deletions.
  1. +21 −6 cake/libs/view/helpers/form.php
  2. +25 −0 cake/tests/cases/libs/view/helpers/form.test.php
View
@@ -73,6 +73,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.
*
* Options:
@@ -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) {
@@ -4712,6 +4712,31 @@ function testFormCreate() {
}
/**
+ * 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 (&)
*
*/

0 comments on commit f9f2986

Please sign in to comment.