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.
This commit is contained in:
ADmad 2013-10-04 20:35:38 +05:30
parent ba1eb7e495
commit aa60b8791a
2 changed files with 30 additions and 13 deletions

View file

@ -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);
}
/**

View file

@ -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);
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;
}