Added "required" attribute to input element based on validation rules for field

This commit is contained in:
ADmad 2012-12-20 01:42:49 +05:30
parent 69248dfb1a
commit 8ef2c22c29
3 changed files with 41 additions and 29 deletions

View file

@ -3577,7 +3577,13 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->radio('Contact.1.imrequired', array('option A'));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
array('input' => array(
'type' => 'radio',
'name' => 'data[Contact][1][imrequired]',
'value' => '0',
'id' => 'Contact1Imrequired0',
'required' => 'required'
)),
'label' => array('for' => 'Contact1Imrequired0'),
'option A',
'/label'
@ -7185,7 +7191,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
'id' => 'ContactImrequired'
'id' => 'ContactImrequired',
'required' => 'required'
),
'/div'
);
@ -7199,7 +7206,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
'id' => 'ContactImalsorequired'
'id' => 'ContactImalsorequired',
'required' => 'required'
),
'/div'
);
@ -7213,7 +7221,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
'id' => 'ContactImrequiredtoo'
'id' => 'ContactImrequiredtoo',
'required' => 'required'
),
'/div'
);
@ -7227,7 +7236,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][required_one]',
'id' => 'ContactRequiredOne'
'id' => 'ContactRequiredOne',
'required' => 'required'
),
'/div'
);
@ -7241,7 +7251,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][string_required]',
'id' => 'ContactStringRequired'
'id' => 'ContactStringRequired',
'required' => 'required'
),
'/div'
);
@ -7311,7 +7322,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
'id' => 'ContactIamrequiredalways'
'id' => 'ContactIamrequiredalways',
'required' => 'required'
),
'/div'
);
@ -8216,7 +8228,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
'id' => 'ContactImrequiredoncreate'
'id' => 'ContactImrequiredoncreate',
'required' => 'required'
),
'/div'
);
@ -8230,12 +8243,14 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
'id' => 'ContactImrequiredonboth'
'id' => 'ContactImrequiredonboth',
'required' => 'required'
),
'/div'
);
$this->assertTags($result, $expected);
$this->Form->inputDefaults(array('required' => false));
$result = $this->Form->input('Contact.imrequired');
$expected = array(
'div' => array('class' => 'input text required'),
@ -8249,6 +8264,9 @@ class FormHelperTest extends CakeTestCase {
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.imrequired', array('required' => false));
$this->assertTags($result, $expected);
}
/**
@ -8268,7 +8286,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
'id' => 'ContactImrequiredonupdate'
'id' => 'ContactImrequiredonupdate',
'required' => 'required'
),
'/div'
);
@ -8295,7 +8314,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
'id' => 'ContactImrequiredonboth'
'id' => 'ContactImrequiredonboth',
'required' => 'required'
),
'/div'
);
@ -8309,7 +8329,8 @@ class FormHelperTest extends CakeTestCase {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
'id' => 'ContactImrequired'
'id' => 'ContactImrequired',
'required' => 'required'
),
'/div'
);

View file

@ -145,7 +145,7 @@ class Helper extends Object {
protected $_minimizedAttributes = array(
'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected',
'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize',
'autoplay', 'controls', 'loop', 'muted'
'autoplay', 'controls', 'loop', 'muted', 'required'
);
/**
@ -418,22 +418,8 @@ class Helper extends Object {
}
/**
* Returns a space-delimited string with items of the $options array. If a
* key of $options array happens to be one of:
*
* - 'compact'
* - 'checked'
* - 'declare'
* - 'readonly'
* - 'disabled'
* - 'selected'
* - 'defer'
* - 'ismap'
* - 'nohref'
* - 'noshade'
* - 'nowrap'
* - 'multiple'
* - 'noresize'
* Returns a space-delimited string with items of the $options array. If a key
* of $options array happens to be one of those listed in `Helper::$_minimizedAttributes`
*
* And its value is one of:
*

View file

@ -2775,6 +2775,11 @@ class FormHelper extends AppHelper {
if (!empty($result['disabled']) || $secure === self::SECURE_SKIP) {
return $result;
}
if (!isset($result['required']) &&
$this->_introspectModel($this->model(), 'validates', $this->field())
) {
$result['required'] = true;
}
$fieldName = null;
if (!empty($options['name'])) {