Skip to content

Commit

Permalink
Fix required field detection.
Browse files Browse the repository at this point in the history
Fix required field detection to match documentation and behavior when
validating. Having `allowEmpty` in the first validation rule, makes the
field not 'required' as the field is allowed to be empty.

Fixes #3194
  • Loading branch information
markstory committed Sep 12, 2012
1 parent 2bbf6bd commit 99a9cc9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -107,8 +107,7 @@ class Contact extends CakeTestModel {
'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
'imrequiredonboth' => array(
'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
'check' => array('rule' => 'alphaNumeric')
'required' => array('rule' => 'alphaNumeric'),
),
'string_required' => 'notEmpty',
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
Expand Down
9 changes: 6 additions & 3 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -244,13 +244,16 @@ protected function _introspectModel($model, $key, $field = null) {
* @return boolean true if field is required to be filled, false otherwise
*/
protected function _isRequiredField($validationRules) {
if (empty($validationRules) || count($validationRules) === 0) {
return false;
}
foreach ($validationRules as $rule) {
$rule->isUpdate($this->requestType === 'put');
if (!$rule->isEmptyAllowed()) {
return true;
if ($rule->isEmptyAllowed()) {
return false;
}
}
return false;
return true;
}

/**
Expand Down

0 comments on commit 99a9cc9

Please sign in to comment.