From 8ef2c22c295f16128835d6478135acded97561c7 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 20 Dec 2012 01:42:49 +0530 Subject: [PATCH] Added "required" attribute to input element based on validation rules for field --- .../Test/Case/View/Helper/FormHelperTest.php | 45 ++++++++++++++----- lib/Cake/View/Helper.php | 20 ++------- lib/Cake/View/Helper/FormHelper.php | 5 +++ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index e48e3a942..798e7d3ec 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -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' ); diff --git a/lib/Cake/View/Helper.php b/lib/Cake/View/Helper.php index 0e54f5fac..c790bd974 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -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: * diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index dc258a0a6..27be0f4ef 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -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'])) {