From d3c3d34dce5a011c8ba309f995b483e99594b3d4 Mon Sep 17 00:00:00 2001 From: nate Date: Mon, 30 Apr 2007 06:24:33 +0000 Subject: [PATCH] Changing 'allowEmpty' for model validation, adding 'required' and enabling multiple rules per field git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4934 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 73 ++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 6d3a1397b..398449559 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1690,41 +1690,49 @@ class Model extends Overloadable { $Validation = new Validation(); - foreach($this->validate as $fieldName => $validator) { - if (!is_array($validator) && isset($data[$fieldName])) { - $validator = array('rule' => $validator); + foreach($this->validate as $fieldName => $ruleSet) { + if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) { + $ruleSet = array($ruleSet); } - $validator = am(array( - 'allowEmpty' => true, - 'message' => 'This field cannot be left blank', - 'rule' => 'blank', - 'on' => null - ), $validator); + foreach ($ruleSet as $validator) { + if (!is_array($validator)) { + $validator = array('rule' => $validator); + } - if (empty($validator['on']) || ($validator['on'] == 'create' && !$this->exists()) || ($validator['on'] == 'update' && $this->exists())) { - if (empty($data[$fieldName]) && $validator['allowEmpty'] == false) { - $this->invalidate($fieldName, $validator['message']); - } elseif (isset($data[$fieldName])) { - if (is_array($validator['rule'])) { - $rule = $validator['rule'][0]; - unset($validator['rule'][0]); - $ruleParams = am(array($data[$fieldName]), array_values($validator['rule'])); - } else { - $rule = $validator['rule']; - $ruleParams = array($data[$fieldName]); - } + $validator = am(array( + 'allowEmpty' => true, + 'required' => false, + 'message' => 'This field cannot be left blank', + 'rule' => 'blank', + 'last' => false, + 'on' => null + ), $validator); - $valid = true; - if (method_exists($this, $rule)) { - $valid = call_user_func_array(array(&$this, $rule), $ruleParams); - } elseif (method_exists($Validation, $rule)) { - $valid = call_user_func_array(array(&$Validation, $rule), $ruleParams); - } elseif (!is_array($validator['rule'])) { - $valid = preg_match($rule, $data[$fieldName]); - } - if (!$valid) { + if (empty($validator['on']) || ($validator['on'] == 'create' && !$this->exists()) || ($validator['on'] == 'update' && $this->exists())) { + if ((!isset($data[$fieldName]) && $validator['required'] == true) || (isset($data[$fieldName]) && empty($data[$fieldName]) && $validator['allowEmpty'] == false)) { $this->invalidate($fieldName, $validator['message']); + } elseif (isset($data[$fieldName])) { + if (is_array($validator['rule'])) { + $rule = $validator['rule'][0]; + unset($validator['rule'][0]); + $ruleParams = am(array($data[$fieldName]), array_values($validator['rule'])); + } else { + $rule = $validator['rule']; + $ruleParams = array($data[$fieldName]); + } + + $valid = true; + if (method_exists($this, $rule)) { + $valid = call_user_func_array(array(&$this, $rule), $ruleParams); + } elseif (method_exists($Validation, $rule)) { + $valid = call_user_func_array(array(&$Validation, $rule), $ruleParams); + } elseif (!is_array($validator['rule'])) { + $valid = preg_match($rule, $data[$fieldName]); + } + if (!$valid) { + $this->invalidate($fieldName, $validator['message']); + } } } } @@ -1738,10 +1746,13 @@ class Model extends Overloadable { * @param mixed $value * @return void */ - function invalidate($field, $value = 1) { + function invalidate($field, $value = null) { if (!is_array($this->validationErrors)) { $this->validationErrors = array(); } + if (empty($value)) { + $value = true; + } $this->validationErrors[$field] = $value; } /**