Fix required field detection.

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
This commit is contained in:
mark_story 2012-09-11 21:59:46 -04:00
parent 2bbf6bd568
commit 99a9cc9669
2 changed files with 7 additions and 5 deletions

View file

@ -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),

View file

@ -244,13 +244,16 @@ class FormHelper extends AppHelper {
* @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;
}
/**