Skip to content

Commit

Permalink
Merge branch '1.3-formhelper' into 1.3-misc
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 17, 2009
2 parents 078b845 + 4e038e5 commit aa0c7cb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 52 deletions.
85 changes: 46 additions & 39 deletions cake/libs/view/helpers/form.php 100644 → 100755
@@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */

/**
* Automatic generation of HTML FORMs from given data.
*
Expand All @@ -9,20 +7,16 @@
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @since CakePHP(tm) v 0.10.0.1076
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/

Expand Down Expand Up @@ -78,16 +72,27 @@ class FormHelper extends AppHelper {
*/
var $requestType = null;

/**
* Persistent default options used by input(). Set by FormHelper::create().
*
* @var array
* @access protected
*/
var $_inputDefaults = array();

/**
* Returns an HTML FORM element.
*
* Options:
* #### Options:
*
* - 'type' Form method defaults to POST
* - 'action' The Action the form submits to. Can be a string or array,
* - '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(). Any options that would
* be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
* can be overridden when calling input()
*
* @access public
* @param string $model The model object which the form is being defined for
Expand Down Expand Up @@ -179,8 +184,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 @@ -607,8 +615,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 @@ -784,10 +795,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 Expand Up @@ -1536,8 +1547,8 @@ function meridian($fieldName, $selected = null, $attributes = array(), $showEmpt
* - 'separator' The contents of the string between select elements. Defaults to '-'
*
* @param string $fieldName Prefix name for the SELECT element
* @param string $dateFormat DMY, MDY, YMD or NONE.
* @param string $timeFormat 12, 24, NONE
* @param string $dateFormat DMY, MDY, YMD.
* @param string $timeFormat 12, 24.
* @param string $selected Option which is selected.
* @param string $attributes array of Attributes
* @param bool $showEmpty Whether or not to show an empty default value.
Expand Down Expand Up @@ -1573,7 +1584,7 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
$days[1] = $selected;
}

if ($timeFormat != 'NONE' && !empty($timeFormat)) {
if (!empty($timeFormat)) {
$time = explode(':', $days[1]);
$check = str_replace(':', '', $days[1]);

Expand Down Expand Up @@ -1635,28 +1646,25 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
}
}

$opt = '';

if ($dateFormat != 'NONE') {
$selects = array();
foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
switch ($char) {
case 'Y':
$selects[] = $this->year(
$fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty
);
break;
case 'M':
$selectMonthAttr['monthNames'] = $monthNames;
$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty);
break;
case 'D':
$selects[] = $this->day($fieldName, $day, $selectDayAttr, $showEmpty);
break;
}
$selects = array();
foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
switch ($char) {
case 'Y':
$selects[] = $this->year(
$fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty
);
break;
case 'M':
$selectMonthAttr['monthNames'] = $monthNames;
$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty);
break;
case 'D':
$selects[] = $this->day($fieldName, $day, $selectDayAttr, $showEmpty);
break;
}
$opt = implode($separator, $selects);
}
$opt = implode($separator, $selects);

if (!empty($interval) && $interval > 1 && !empty($min)) {
$min = round($min * (1 / $interval)) * $interval;
}
Expand All @@ -1671,7 +1679,6 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
$this->minute($fieldName, $min, $selectMinuteAttr, $showEmpty) . ' ' .
$this->meridian($fieldName, $meridian, $selectMeridianAttr, $showEmpty);
break;
case 'NONE':
default:
$opt .= '';
break;
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/prototype_engine.php
Expand Up @@ -37,7 +37,7 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
**/
var $_optionMap = array(
'request' => array(
'async' => 'asyncrhronous',
'async' => 'asynchronous',
'data' => 'parameters',
'before' => 'onCreate',
'success' => 'onSuccess',
Expand Down
3 changes: 1 addition & 2 deletions cake/tests/cases/libs/model/model_integration.test.php
Expand Up @@ -2,7 +2,7 @@
/* SVN FILE: $Id: model.test.php 8225 2009-07-08 03:25:30Z mark_story $ */

/**
* ModelDeleteTest file
* ModelIntegrationTest file
*
* Long description for file
*
Expand All @@ -26,7 +26,6 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
require_once dirname(__FILE__) . DS . 'model.test.php';
require_once dirname(__FILE__) . DS . 'model_integration.test.php';

/**
* ModelIntegrationTest
Expand Down
1 change: 0 additions & 1 deletion cake/tests/cases/libs/model/model_validation.test.php
Expand Up @@ -26,7 +26,6 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
require_once dirname(__FILE__) . DS . 'model.test.php';
require_once dirname(__FILE__) . DS . 'model_validation.test.php';

/**
* ModelValidationTest
Expand Down
35 changes: 27 additions & 8 deletions cake/tests/cases/libs/view/helpers/form.test.php
@@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */

/**
* FormHelperTest file
*
Expand All @@ -9,20 +7,16 @@
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2006-2008, Cake Software Foundation, Inc.
* Copyright 2006-2009, Cake Software Foundation, Inc.
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
Expand Down Expand Up @@ -4717,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
13 changes: 12 additions & 1 deletion cake/tests/cases/libs/view/helpers/prototype_engine.test.php
Expand Up @@ -227,7 +227,7 @@ function testRequest() {
));
$expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:doSuccess, onFailure:handleError, parameters:$("element").serialize()});';
$this->assertEqual($result, $expected);

$result = $this->Proto->request('/people/edit/1', array(
'method' => 'post',
'before' => 'doBefore();',
Expand All @@ -237,6 +237,17 @@ function testRequest() {
));
$expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});';
$this->assertEqual($result, $expected);

$result = $this->Proto->request('/people/edit/1', array(
'async' => false,
'method' => 'post',
'before' => 'doBefore();',
'success' => 'doSuccess();',
'complete' => 'doComplete();',
'error' => 'handleError();',
));
$expected = 'var jsRequest = new Ajax.Request("/people/edit/1", {asynchronous:false, method:"post", onComplete:function (transport) {doComplete();}, onCreate:function (transport) {doBefore();}, onFailure:function (response, jsonHeader) {handleError();}, onSuccess:function (response, jsonHeader) {doSuccess();}});';
$this->assertEqual($result, $expected);
}

/**
Expand Down

0 comments on commit aa0c7cb

Please sign in to comment.