Skip to content
Permalink
Browse files

Improving the guessing process of required fields in the form helper.

Now it does not rely on the "required" property of the validation array anymore, which was misleading
  • Loading branch information...
lorenzo committed Feb 23, 2010
1 parent 753721c commit 9a4de854d602b307fbd1ee48b525877abcf2e07f
Showing with 68 additions and 15 deletions.
  1. +35 −12 cake/libs/view/helpers/form.php
  2. +33 −3 cake/tests/cases/libs/view/helpers/form.test.php
@@ -123,28 +123,51 @@ function &_introspectModel($model) {
$validates = array();
if (!empty($object->validate)) {
foreach ($object->validate as $validateField => $validateProperties) {
if (is_array($validateProperties)) {
$dims = Set::countDim($validateProperties);
if (($dims == 1 && !isset($validateProperties['required']) || (array_key_exists('required', $validateProperties) && $validateProperties['required'] !== false))) {
$validates[] = $validateField;
} elseif ($dims > 1) {
foreach ($validateProperties as $rule => $validateProp) {
if (is_array($validateProp) && (array_key_exists('required', $validateProp) && $validateProp['required'] !== false)) {
$validates[] = $validateField;
}
}
}
if ($this->_isRequiredField($validateProperties)) {
$validates[] = $validateField;
}
}
}
$defaults = array('fields' => array(), 'key' => 'id', 'validates' => array());
$key = $object->primaryKey;
$this->fieldset[$object->name] = array_merge($defaults,compact('fields', 'key', 'validates'));
$this->fieldset[$object->name] = array_merge($defaults, compact('fields', 'key', 'validates'));
}
return $object;
}
/**
* Returns if a field is required to be filled based on validation properties from the validating object
*
* @return boolean true if field is required to be filled, false otherwise
* @access protected
*/
function _isRequiredField($validateProperties) {
$required = false;
if (is_array($validateProperties)) {
$dims = Set::countDim($validateProperties);
if ($dims == 1) {
$validateProperties = array($validateProperties);
}
foreach ($validateProperties as $rule => $validateProp) {
$rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
if ($rule) {
$rule = is_array($rule) ? array_shift($rule) : $rule;
$rule = is_string($rule) ? strtolower($rule) : $rule;
}
$required = empty($validateProp);
$required = $required || (!empty($rule) && $rule === 'notempty');
$required = $required || (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] !== true);
if ($required) {
break;
}
}
}
return $required;
}
/**
* Returns an HTML FORM element.
*
@@ -104,10 +104,12 @@ class Contact extends CakeTestModel {
var $validate = array(
'non_existing' => array(),
'idontexist' => array(),
'imrequired' => array('rule' => array('between', 5, 30), 'required' => true),
'imalsorequired' => array('rule' => 'alphaNumeric', 'required' => true),
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
'imrequiredtoo' => array('rule' => 'notEmpty'),
'required_one' => array('required' => array('rule' => array('notEmpty'))),
'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric'),
'imalsonotrequired' => array('alpha' => array('rule' => 'alphaNumeric','required' => false),
'imalsonotrequired' => array('alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
'between' => array('rule' => array('between', 5, 30))));
/**
@@ -5406,6 +5408,34 @@ function testFormMagicInput() {
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imrequiredtoo');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactImrequiredtoo'),
'Imrequiredtoo',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
'id' => 'ContactImrequiredtoo'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.required_one');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactRequiredOne'),
'Required One',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][required_one]',
'id' => 'ContactRequiredOne'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imnotrequired');
$expected = array(
'div' => array('class' => 'input text'),

0 comments on commit 9a4de85

Please sign in to comment.
You can’t perform that action at this time.