Permalink
Browse files

Added "required" attribute to input element based on validation rules…

… for field
  • Loading branch information...
ADmad committed Dec 19, 2012
1 parent 69248df commit 8ef2c22c295f16128835d6478135acded97561c7
Showing with 41 additions and 29 deletions.
  1. +33 −12 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +3 −17 lib/Cake/View/Helper.php
  3. +5 −0 lib/Cake/View/Helper/FormHelper.php
@@ -3577,7 +3577,13 @@ public function testRadio() {
$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 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
- 'id' => 'ContactImrequired'
+ 'id' => 'ContactImrequired',
+ 'required' => 'required'
),
'/div'
);
@@ -7199,7 +7206,8 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
- 'id' => 'ContactImalsorequired'
+ 'id' => 'ContactImalsorequired',
+ 'required' => 'required'
),
'/div'
);
@@ -7213,7 +7221,8 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
- 'id' => 'ContactImrequiredtoo'
+ 'id' => 'ContactImrequiredtoo',
+ 'required' => 'required'
),
'/div'
);
@@ -7227,7 +7236,8 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][required_one]',
- 'id' => 'ContactRequiredOne'
+ 'id' => 'ContactRequiredOne',
+ 'required' => 'required'
),
'/div'
);
@@ -7241,7 +7251,8 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][string_required]',
- 'id' => 'ContactStringRequired'
+ 'id' => 'ContactStringRequired',
+ 'required' => 'required'
),
'/div'
);
@@ -7311,7 +7322,8 @@ public function testFormInputRequiredDetection() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
- 'id' => 'ContactIamrequiredalways'
+ 'id' => 'ContactIamrequiredalways',
+ 'required' => 'required'
),
'/div'
);
@@ -8216,7 +8228,8 @@ public function testRequiredOnCreate() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
- 'id' => 'ContactImrequiredoncreate'
+ 'id' => 'ContactImrequiredoncreate',
+ 'required' => 'required'
),
'/div'
);
@@ -8230,12 +8243,14 @@ public function testRequiredOnCreate() {
'/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 @@ public function testRequiredOnCreate() {
'/div'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->input('Contact.imrequired', array('required' => false));
+ $this->assertTags($result, $expected);
}
/**
@@ -8268,7 +8286,8 @@ public function testRequiredOnUpdate() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
- 'id' => 'ContactImrequiredonupdate'
+ 'id' => 'ContactImrequiredonupdate',
+ 'required' => 'required'
),
'/div'
);
@@ -8295,7 +8314,8 @@ public function testRequiredOnUpdate() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
- 'id' => 'ContactImrequiredonboth'
+ 'id' => 'ContactImrequiredonboth',
+ 'required' => 'required'
),
'/div'
);
@@ -8309,7 +8329,8 @@ public function testRequiredOnUpdate() {
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][imrequired]',
- 'id' => 'ContactImrequired'
+ 'id' => 'ContactImrequired',
+ 'required' => 'required'
),
'/div'
);
View
@@ -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 @@ public function clean($output) {
}
/**
- * 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:
*
@@ -2775,6 +2775,11 @@ protected function _initInputField($field, $options = array()) {
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'])) {

0 comments on commit 8ef2c22

Please sign in to comment.