Fix validation detection in FormHelper

string values for validation means a single validation method.
That implies the field is required.

Fixes #2181
This commit is contained in:
mark_story 2011-10-28 21:35:43 -04:00
parent 11e2ef6674
commit ce9bb6b632
2 changed files with 140 additions and 113 deletions

View file

@ -104,6 +104,7 @@ class Contact extends CakeTestModel {
'non_existing' => array(),
'idontexist' => array(),
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
'string_required' => 'notEmpty',
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
'imrequiredtoo' => array('rule' => 'notEmpty'),
'required_one' => array('required' => array('rule' => array('notEmpty'))),
@ -6584,6 +6585,141 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
}
/**
* Test that required fields are created for various types of validation.
*
* @return void
*/
function testFormInputRequiredDetection() {
$this->Form->create('Contact');
$result = $this->Form->input('Contact.non_existing');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactNonExisting'),
'Non Existing',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][non_existing]',
'id' => 'ContactNonExisting'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imrequired');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactImrequired'),
'Imrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
'id' => 'ContactImrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imalsorequired');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactImalsorequired'),
'Imalsorequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
'id' => 'ContactImalsorequired'
),
'/div'
);
$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.string_required');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactStringRequired'),
'String Required',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][string_required]',
'id' => 'ContactStringRequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imnotrequired');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactImnotrequired'),
'Imnotrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
'id' => 'ContactImnotrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imalsonotrequired');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactImalsonotrequired'),
'Imalsonotrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
'id' => 'ContactImalsonotrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imnotrequiredeither');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactImnotrequiredeither'),
'Imnotrequiredeither',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
'id' => 'ContactImnotrequiredeither'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormMagicInput method
*
@ -6671,117 +6807,6 @@ class FormHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.non_existing');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactNonExisting'),
'Non Existing',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][non_existing]',
'id' => 'ContactNonExisting'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imrequired');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactImrequired'),
'Imrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
'id' => 'ContactImrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imalsorequired');
$expected = array(
'div' => array('class' => 'input text required'),
'label' => array('for' => 'ContactImalsorequired'),
'Imalsorequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
'id' => 'ContactImalsorequired'
),
'/div'
);
$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'),
'label' => array('for' => 'ContactImnotrequired'),
'Imnotrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
'id' => 'ContactImnotrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imalsonotrequired');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactImalsonotrequired'),
'Imalsonotrequired',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
'id' => 'ContactImalsonotrequired'
),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imnotrequiredeither');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactImnotrequiredeither'),
'Imnotrequiredeither',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
'id' => 'ContactImnotrequiredeither'
),
'/div'
);
$this->assertTags($result, $expected);
extract($this->dateRegex);
$now = strtotime('now');

View file

@ -239,14 +239,16 @@ class FormHelper extends AppHelper {
}
/**
* Returns if a field is required to be filled based on validation properties from the validating object
* Returns if a field is required to be filled based on validation properties from the validating object.
*
* @param array $validateProperties
* @return boolean true if field is required to be filled, false otherwise
*/
protected function _isRequiredField($validateProperties) {
$required = false;
if (is_array($validateProperties)) {
if (is_string($validateProperties)) {
return true;
} elseif (is_array($validateProperties)) {
$dims = Set::countDim($validateProperties);
if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {