From aa60b8791a213acc1a3ffbc5c6d41e5c12e74727 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 4 Oct 2013 20:35:38 +0530 Subject: [PATCH] Fixed setting "required" attribute for file input. Closes #4124 In general fixed the issue where enabling SECURE_SKIP for a field skipped "required" attribute check altogether. Instead now "required" is set to false for hidden fields by default. --- .../Test/Case/View/Helper/FormHelperTest.php | 16 +++++++++++ lib/Cake/View/Helper/FormHelper.php | 27 ++++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 0959c9ce6..3543f03cb 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -8184,6 +8184,22 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + + $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file')); + $expected = array( + 'div' => array('class' => 'input file required'), + 'label' => array('for' => 'ContactIamrequiredalways'), + 'Iamrequiredalways', + '/label', + 'input' => array( + 'type' => 'file', + 'name' => 'data[Contact][iamrequiredalways]', + 'id' => 'ContactIamrequiredalways', + 'required' => 'required' + ), + '/div' + ); + $this->assertTags($result, $expected); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 377728b8f..295a622bd 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1625,17 +1625,16 @@ class FormHelper extends AppHelper { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hidden */ public function hidden($fieldName, $options = array()) { - $secure = true; + $options += array('required' => false, 'secure' => true); + + $secure = $options['secure']; + unset($options['secure']); - if (isset($options['secure'])) { - $secure = $options['secure']; - unset($options['secure']); - } $options = $this->_initInputField($fieldName, array_merge( $options, array('secure' => self::SECURE_SKIP) )); - if ($secure && $secure !== self::SECURE_SKIP) { + if ($secure === true) { $this->_secure(true, null, '' . $options['value']); } @@ -2012,12 +2011,8 @@ class FormHelper extends AppHelper { $tag = 'selectstart'; } - if ($tag !== 'checkboxmultiplestart' && - !isset($attributes['required']) && - empty($attributes['disabled']) && - $this->_introspectModel($this->model(), 'validates', $this->field()) - ) { - $attributes['required'] = true; + if ($tag === 'checkboxmultiplestart') { + unset($attributes['required']); } if (!empty($tag) || isset($template)) { @@ -2847,15 +2842,21 @@ class FormHelper extends AppHelper { if ($this->tagIsInvalid() !== false) { $result = $this->addClass($result, 'form-error'); } - if (!empty($result['disabled']) || $secure === self::SECURE_SKIP) { + + if (!empty($result['disabled'])) { return $result; } + if (!isset($result['required']) && $this->_introspectModel($this->model(), 'validates', $this->field()) ) { $result['required'] = true; } + if ($secure === self::SECURE_SKIP) { + return $result; + } + $this->_secure($secure, $this->_secureFieldName($options)); return $result; }