Skip to content

Commit

Permalink
Change method to only return true/false.
Browse files Browse the repository at this point in the history
null/true/false is complicated.  Fix issues with some
of the test fixtures.  imalsonotrequired was actually required
as it's last rule didn't specify allowEmpty = true and had a range
validation rule.
  • Loading branch information
markstory committed Mar 22, 2012
1 parent cabb0d0 commit 69e63b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
9 changes: 6 additions & 3 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -106,15 +106,18 @@ class Contact extends CakeTestModel {
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
'imrequiredonboth' => array('required' => array('rule' => 'alphaNumeric')),
'imrequiredonboth' => array(
'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
'check' => array('rule' => 'alphaNumeric')
),
'string_required' => 'notEmpty',
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
'imrequiredtoo' => array('rule' => 'notEmpty'),
'required_one' => array('required' => array('rule' => array('notEmpty'))),
'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
'imalsonotrequired' => array(
'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
'between' => array('rule' => array('between', 5, 30)),
'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
),
'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
);
Expand Down
56 changes: 30 additions & 26 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -249,10 +249,8 @@ protected function _isRequiredField($validateProperties) {
}

foreach ($validateProperties as $rule => $validateProp) {
$parse = $this->_parseRuleRequired($validateProp);
if ($parse === false) {
return false;
} elseif ($parse == null){
$isRequired = $this->_isRequiredRule($validateProp);
if ($isRequired === false) {
continue;
}
$rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
Expand All @@ -265,6 +263,34 @@ protected function _isRequiredField($validateProperties) {
return $required;
}

/**
* Checks if the field is required by the 'on' key in validation properties.
* If no 'on' key is present in validation props, this method returns true.
*
* @param array $validateProp
* @return mixed. Boolean for required
*/
protected function _isRequiredRule($validateProp) {
if (isset($validateProp['on'])) {
if (
($validateProp['on'] == 'create' && $this->requestType != 'post') ||
($validateProp['on'] == 'update' && $this->requestType != 'put')
) {
return false;
}
}
if (
isset($validateProp['allowEmpty']) &&
$validateProp['allowEmpty'] === true
) {
return false;
}
if (isset($validateProp['required']) && empty($validateProp['required'])) {
return false;
}
return true;
}

/**
* Returns false if given form field described by the current entity has no errors.
* Otherwise it returns the validation message
Expand Down Expand Up @@ -2587,26 +2613,4 @@ protected function _initInputField($field, $options = array()) {
return $result;
}

/**
* Checks if the field is required by the 'on' key in validation properties.
* If no 'on' key is present in validation props, this method returns true.
*
* @param array $validateProp
* @return mixed. Boolean for required, null if create/update does not match
*/
protected function _parseRuleRequired($validateProp) {
if (isset($validateProp['on'])) {
if (
($validateProp['on'] == 'create' && $this->requestType != 'post') ||
($validateProp['on'] == 'update' && $this->requestType != 'put')
) {
return null;
}
}
if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) {
return false;
}
return true;
}

}

0 comments on commit 69e63b1

Please sign in to comment.