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
This commit is contained in:
nate 2007-04-30 06:24:33 +00:00
parent c62c550e3f
commit d3c3d34dce

View file

@ -1690,20 +1690,27 @@ class Model extends Overloadable {
$Validation = new Validation(); $Validation = new Validation();
foreach($this->validate as $fieldName => $validator) { foreach($this->validate as $fieldName => $ruleSet) {
if (!is_array($validator) && isset($data[$fieldName])) { if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
$ruleSet = array($ruleSet);
}
foreach ($ruleSet as $validator) {
if (!is_array($validator)) {
$validator = array('rule' => $validator); $validator = array('rule' => $validator);
} }
$validator = am(array( $validator = am(array(
'allowEmpty' => true, 'allowEmpty' => true,
'required' => false,
'message' => 'This field cannot be left blank', 'message' => 'This field cannot be left blank',
'rule' => 'blank', 'rule' => 'blank',
'last' => false,
'on' => null 'on' => null
), $validator); ), $validator);
if (empty($validator['on']) || ($validator['on'] == 'create' && !$this->exists()) || ($validator['on'] == 'update' && $this->exists())) { if (empty($validator['on']) || ($validator['on'] == 'create' && !$this->exists()) || ($validator['on'] == 'update' && $this->exists())) {
if (empty($data[$fieldName]) && $validator['allowEmpty'] == false) { if ((!isset($data[$fieldName]) && $validator['required'] == true) || (isset($data[$fieldName]) && empty($data[$fieldName]) && $validator['allowEmpty'] == false)) {
$this->invalidate($fieldName, $validator['message']); $this->invalidate($fieldName, $validator['message']);
} elseif (isset($data[$fieldName])) { } elseif (isset($data[$fieldName])) {
if (is_array($validator['rule'])) { if (is_array($validator['rule'])) {
@ -1729,6 +1736,7 @@ class Model extends Overloadable {
} }
} }
} }
}
return $this->validationErrors; return $this->validationErrors;
} }
/** /**
@ -1738,10 +1746,13 @@ class Model extends Overloadable {
* @param mixed $value * @param mixed $value
* @return void * @return void
*/ */
function invalidate($field, $value = 1) { function invalidate($field, $value = null) {
if (!is_array($this->validationErrors)) { if (!is_array($this->validationErrors)) {
$this->validationErrors = array(); $this->validationErrors = array();
} }
if (empty($value)) {
$value = true;
}
$this->validationErrors[$field] = $value; $this->validationErrors[$field] = $value;
} }
/** /**