How to display Variable in Error Messages during Data Validation?
Hi,
Anyone know how to display Variable in Error Messages during Data Validation?
for example, for the code below in MODEL
var $validate = array(
'login' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.'
)
);
When it violate the rule, it would display
> Usernames must only contain letters and numbers.
Let's say If the user enter 'hello123...', If I want it to display the data entered instead of just 'Usernames', for example
> 'hello123...' must only contain letters and numbers.
Is it possible?
Thanks!!!
Asked by tfliam, on 24/12/09
1 Answer
I'm not 100% sure, but I believe the answer to this is you can't do this at the model validation specification level but instead have to use str_replace or regexp.
So for example you try to save and get invalidFields (controller level)
$this->MyModel->save($this->data);
$errors = $this->MyModel->invalidFields();
Then you would need to search and replace the keywords with whichever variables you needed (assume that the username is stored in var $username)
if (isset($errors['login'])){
$errors['login'] = str_replace('Usernames', $username, $errors['login']);
}
Then you can pass these up to the view for the user to see.
Answered by davidwuon 28/12/09
Your Answer
You can use Creole Wiki Syntax to format your text.
Tagged with
Rating
0
Viewed
254 times
Last Activity
on 28/12/09