updating FormHelper::inputs(), closes #3325 and closes #3324, tests added

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5758 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-10-14 23:41:05 +00:00
parent b61f559030
commit a3fc6e4af5
2 changed files with 42 additions and 33 deletions

View file

@ -38,7 +38,8 @@ class FormHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var unknown_type
* @var array
* @access public
*/
var $helpers = array('Html');
/**
@ -51,7 +52,7 @@ class FormHelper extends AppHelper {
/**
* Enter description here...
*
* @var unknown_type
* @var array
*/
var $__options = array(
'day' => array(), 'minute' => array(), 'hour' => array(),
@ -61,12 +62,14 @@ class FormHelper extends AppHelper {
* Enter description here...
*
* @var array
* @access public
*/
var $fields = array();
/**
* Defines the type of form being created. Set by FormHelper::create().
*
* @var string
* @access public
*/
var $requestType = null;
/**
@ -145,7 +148,6 @@ class FormHelper extends AppHelper {
$created = true;
$id = $this->data[$model][$data['key']];
}
$view->modelId = $id;
$options = am(array(
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
'action' => null,
@ -401,7 +403,7 @@ class FormHelper extends AppHelper {
if (substr($text, -3) == '_id') {
$text = substr($text, 0, strlen($text) - 3);
}
$text = Inflector::humanize($text);
$text = Inflector::humanize(Inflector::underscore($text));
}
if (isset($attributes['for'])) {
@ -424,29 +426,36 @@ class FormHelper extends AppHelper {
* @param array $blacklist a simple array of fields to skip
* @return output
*/
function inputs($fields = null, $blacklist = null) {
if (!is_array($fields)) {
$fieldset = $fields;
$fields = array_keys($this->fieldset['fields']);
}
if (isset($fields['fieldset'])) {
$fieldset = $fields['fieldset'];
unset($fields['fieldset']);
function inputs($fields = true, $blacklist = null) {
if (is_array($fields)) {
if (array_key_exists('legend', $fields)) {
$legend = $fields['legend'];
unset($fields['legend']);
}
if (isset($fields['fieldset'])) {
$fields = $fields['fieldset'];
unset($fields['fieldset']);
}
} else {
$fieldset = true;
$legend = $fields;
unset($fields);
}
if ($fieldset === true) {
if (empty($fields)) {
$fields = array_keys($this->fieldset['fields']);
}
if ($legend === true) {
$legend = 'New ';
if (in_array($this->action, array('update', 'edit'))) {
$prefix = null;
if (isset($this->params['prefix'])) {
$prefix = $this->params['prefix'];
}
if (in_array(str_replace($prefix .'_', '', $this->action), array('update', 'edit'))) {
$legend = 'Edit ';
}
$legend .= Inflector::humanize(Inflector::underscore($this->model()));
} elseif (is_string($fieldset)) {
$legend = $fieldset;
} elseif (isset($fieldset['legend'])) {
$legend = $fields['legend'];
unset($fields['legend']);
}
$out = null;
@ -455,18 +464,6 @@ class FormHelper extends AppHelper {
$name = $options;
$options = array();
}
if (is_array($options) && isset($options['fieldset'])) {
$out .= $this->inputs($options);
continue;
}
if (is_array($options) && isset($options['fieldName'])) {
$name = $options['fieldName'];
unset($options['fieldName']);
}
if (isset($options['blacklist'])) {
$blacklist = $options['blacklist'];
unset($options['blacklist']);
}
if (is_array($blacklist) && in_array($name, $blacklist)) {
continue;
}

View file

@ -362,6 +362,18 @@ class FormHelperTest extends CakeTestCase {
$this->assertNoPattern('/error-message/', $result);
}
function testFormInputs() {
$this->Form->create('Contact');
$result = $this->Form->inputs('The Legend');
$this->assertPattern('/<legend>The Legend<\/legend>/', $result);
$this->Form->params['prefix'] = 'admin';
$this->Form->action = 'admin_edit';
$result = $this->Form->inputs();
$this->assertPattern('/<legend>Edit Contact<\/legend>/', $result);
}
function testLabel() {
$this->Form->text('Person/name');
$result = $this->Form->label();
@ -480,7 +492,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertNoPattern('/checked="checked"/', $result);
$this->assertPattern('/^<fieldset><legend>field<\/legend>(<input[^<>]+><label[^<>]+>option [AB]<\/label>)+<\/fieldset>$/', $result);
$this->assertPattern('/(<input[^<>]+name="data\[Model\]\[field\]"[^<>]+>.+){2}/', $result);
$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
$this->assertPattern('/id="Field0"/', $result);
$this->assertPattern('/id="Field1"/', $result);