Browse Source

Change method to only return true/false.

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.
mark_story 14 years ago
parent
commit
69e63b11a4

+ 6 - 3
lib/Cake/Test/Case/View/Helper/FormHelperTest.php

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

+ 30 - 26
lib/Cake/View/Helper/FormHelper.php

@@ -249,10 +249,8 @@ class FormHelper extends AppHelper {
 			}
 
 			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;
@@ -266,6 +264,34 @@ class FormHelper extends AppHelper {
 	}
 
 /**
+ * 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
  *
@@ -2587,26 +2613,4 @@ class FormHelper extends AppHelper {
 		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;
-	}
-
 }