Skip to content

Commit

Permalink
Refactored FormHelper to take advantage of the new validator object
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 11, 2012
1 parent 217bf85 commit fe659c0
Showing 1 changed file with 8 additions and 52 deletions.
60 changes: 8 additions & 52 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -218,7 +218,7 @@ protected function _introspectModel($model, $key, $field = null) {
if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) { if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
$validates = array(); $validates = array();
if (!empty($object->validate)) { if (!empty($object->validate)) {
foreach ($object->validate as $validateField => $validateProperties) { foreach ($object->validator() as $validateField => $validateProperties) {
if ($this->_isRequiredField($validateProperties)) { if ($this->_isRequiredField($validateProperties)) {
$validates[$validateField] = true; $validates[$validateField] = true;
} }
Expand All @@ -240,61 +240,17 @@ protected function _introspectModel($model, $key, $field = null) {
/** /**
* Returns if a field is required to be filled based on validation properties from the validating object. * Returns if a field is required to be filled based on validation properties from the validating object.
* *
* @param array $validateProperties * @param CakeValidationSet $validationRules
* @return boolean true if field is required to be filled, false otherwise * @return boolean true if field is required to be filled, false otherwise
*/ */
protected function _isRequiredField($validateProperties) { protected function _isRequiredField($validationRules) {
$required = false; foreach ($validationRules as $rule) {
if (is_string($validateProperties)) { $rule->isUpdate($this->requestType === 'put');
return true; if (!$rule->isEmptyAllowed()) {
} elseif (is_array($validateProperties)) { return true;

$dims = Hash::dimensions($validateProperties);
if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {
$validateProperties = array($validateProperties);
}

foreach ($validateProperties as $rule => $validateProp) {
$isRequired = $this->_isRequiredRule($validateProp);
if ($isRequired === false) {
continue;
}
$rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
$required = $rule || empty($validateProp);
if ($required) {
break;
}
} }
} }
return $required; return false;
}

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


/** /**
Expand Down

0 comments on commit fe659c0

Please sign in to comment.