Skip to content

Commit

Permalink
Implementing Form helper default options. Allows the creation of pers…
Browse files Browse the repository at this point in the history
…istent form options when opening a form.

Adding patch from 'Ceeram' / 'bankai'
Refs #56.
  • Loading branch information
markstory committed Oct 17, 2009
1 parent 6dcc819 commit f9f2986
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
27 changes: 21 additions & 6 deletions cake/libs/view/helpers/form.php 100644 → 100755
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -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 (&)
*
Expand Down

0 comments on commit f9f2986

Please sign in to comment.