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'])) {
$validates = array();
if (!empty($object->validate)) {
foreach ($object->validate as $validateField => $validateProperties) {
foreach ($object->validator() as $validateField => $validateProperties) {
if ($this->_isRequiredField($validateProperties)) {
$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.
*
* @param array $validateProperties
* @param CakeValidationSet $validationRules
* @return boolean true if field is required to be filled, false otherwise
*/
protected function _isRequiredField($validateProperties) {
$required = false;
if (is_string($validateProperties)) {
return true;
} elseif (is_array($validateProperties)) {

$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;
}
protected function _isRequiredField($validationRules) {
foreach ($validationRules as $rule) {
$rule->isUpdate($this->requestType === 'put');
if (!$rule->isEmptyAllowed()) {
return true;
}
}
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;
return false;
}

/**
Expand Down

0 comments on commit fe659c0

Please sign in to comment.