Adding overriding error messages when multiple rules

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4948 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-04-30 12:10:57 +00:00
parent d029a776b7
commit 30a5f0384e
2 changed files with 19 additions and 4 deletions

View file

@ -1695,7 +1695,7 @@ class Model extends Overloadable {
$ruleSet = array($ruleSet);
}
foreach ($ruleSet as $validator) {
foreach ($ruleSet as $index => $validator) {
if (!is_array($validator)) {
$validator = array('rule' => $validator);
}
@ -1703,15 +1703,20 @@ class Model extends Overloadable {
$validator = am(array(
'allowEmpty' => true,
'required' => false,
'message' => 'This field cannot be left blank',
'rule' => 'blank',
'last' => false,
'on' => null
), $validator);
if (isset($validator['message'])) {
$message = $validator['message'];
} else {
$message = 'This field cannot be left blank';
}
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]) && $data[$fieldName] != 0) && $validator['allowEmpty'] == false)) {
$this->invalidate($fieldName, $validator['message']);
$this->invalidate($fieldName, $message);
} elseif (isset($data[$fieldName])) {
if (is_array($validator['rule'])) {
$rule = $validator['rule'][0];
@ -1731,6 +1736,10 @@ class Model extends Overloadable {
$valid = preg_match($rule, $data[$fieldName]);
}
if (!$valid) {
if (!isset($validator['message'])) {
$validator['message'] = ife(is_string($index) || (is_numeric($index) && $index > 0), $index, $message);
}
$this->invalidate($fieldName, $validator['message']);
}
}
@ -2122,4 +2131,4 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
Overloadable::overload('Model');
}
?>
?>

View file

@ -271,6 +271,12 @@ class FormHelper extends AppHelper {
$options = am(array('wrap' => true, 'class' => 'error-message', 'escape' => true), $options);
if ($error = $this->tagIsInvalid()) {
if (is_array($text) && isset($text[$error])) {
$text = $text[$error];
} else if (is_array($text)) {
$text = null;
}
if ($text != null) {
$error = $text;
} elseif (is_numeric($error)) {