Hi,
Is there any way to do $model->validate() without having errors added? I just want to know the true og false value, but not add errors as the user may not even have entered data yet.
Thanks.
Hi,
Is there any way to do $model->validate() without having errors added? I just want to know the true og false value, but not add errors as the user may not even have entered data yet.
Thanks.
You could call $model->validate() and then $model->clearErrors() before the form is redisplayed.
Matt
I know, but if the code is executed after a real validation of user input, the errors shouldn’t be cleared.
You could write a function in your model, or a component if you’ll need it across the entire app, to validate and then clean errors.
public function isValid()
{
$isValid = false;
if ($this->validate())
$isValid = true;
$this->clearErrors();
return $isValid;
}
Matt
I dont’ think you understand the problem. I can’t do “clearErrors()”, because their may be data there.
Would this work?
public function isValid()
{
$errors = $this->getErrors();
$isValid = false;
if ($this->validate())
$isValid = true;
$this->clearErrors();
// Psuedo foreach $errors : $this->addError($error);
return $isValid;
}
Yes it would, but is is not that elegant.
You might also want to look into beforeValidate and afterValidate
http://www.yiiframework.com/doc/api/1.1/CModel#beforeValidate-detail
http://www.yiiframework.com/doc/api/1.1/CModel#afterValidate-detail
What solution did you decide to go with? Mind sharing?
Matt
I haven’t implemented a solution yet. I still think the only way to really do this, is support a silent parameter in CActiveRecord::validate.
I don’t understand your need… you want to validate but not to display error… and at the same time you want to display error is there is some data…
can you explain a bit more… when would you like to validate but not display errors… and when would you like to validate and display errors…