slyder
(K Gorbachov)
June 26, 2009, 9:15am
1
I want to add my own error to model using addError method.
After using $curruser->addError('someid', 'You already logged as defined user.');
var_dump($curruser->getErrors()); returns array(1) { ["someid"]=> array(1) { [0]=> string(35) "You already logged as defined user." } }
but
var_dump($curruser->validate()); have bool(true)
is there any way to add my own error? or i should use validate rules with my own validator for this issue?
$curruser = $this->loadUser(Yii::app()->user->id);
$curruser->gal_tree = $sourceUser->gal_tree;
$curruser->setScenario('setGalleryTree');
$curruser->addError('someid', 'You already logged as defined user.');
var_dump($curruser->getErrors());
var_dump($curruser->validate());
qiang
(Qiang Xue)
June 26, 2009, 8:19pm
2
When you call validate(), it will clear all existing error messages.
You should mainly use addError() in a validator.
slyder
(K Gorbachov)
June 29, 2009, 10:04am
3
I've overided beforeValidate function in model and it works now, thanks!
protected function beforeValidate($scenario) {
$r = parent::beforeValidate($scenario);
if ($scenario == 'copyGallery') {
...
}
return $r;
}
But what if i want to pass some parameters from controller (i do not needed it now, but probably in future =)) ? I should to use some additional fields in model for that?
I think it will be good practise if here will be possible to add some validation or errors from controller? It is just an idea.
Thanks again!
foe_1
(foe#1)
July 6, 2009, 5:07am
4
Logically this seems to be possible, but does not work
$model = new Vendor;
$model->attrib = false;
if ( !$model->attrib ) {
Vendor::model()->addError('attrib', 'Error added in Controller: attrib must be true.');
OR
$model->addError('attrib', 'Error added in Controller: attrib must be true.');
}
slyder
(K Gorbachov)
July 8, 2009, 8:48am
5
Yes, i've tryed so, but as said qiang validate method clears massive with errors before validation.
Here is possible way to add custom errors:
add addCustomError method with next code:
$this->customErrors[] = array('id', 'desc')
and use this customErrors array in beforeValidate method.
waylex
(Sergeymorkovkin)
September 2, 2009, 6:38pm
6
I faced this problem a month ago and decided to write custom methods such as validateDetails($param1, $param2, &$out).
Validation now looks like the following:
if ($form->validate() && $form->validateDetails($param1, $param2, &$out))
{
// ...
}
Unfortunately, no better solution to keep things separate.
However, if you need simplicity, you may call addError after
validate() in a controller action code and things will work
fine.
kylian
(Yvan Sanchez)
February 8, 2011, 4:19pm
7
slyder:
Yes, i’ve tryed so, but as said qiang validate method clears massive with errors before validation.
Here is possible way to add custom errors:
add addCustomError method with next code:
$this->customErrors[] = array(‘id’, ‘desc’)
and use this customErrors array in beforeValidate method.
I tried your way and here is my resulting code (it works!)
Thank you for your idea. Put this code into your model class:
public $customErrors=array();
/**
*/
public function addCustomError($attribute, $error) {
$this->customErrors[] = array($attribute, $error);
}
/**
*/
protected function beforeValidate() {
$r = parent::beforeValidate();
// if ($this->scenario == 'xxx') {
// ...
// }
foreach ($this->customErrors as $param) {
$this->addError($param[0], $param[1]);
}
return $r;
}
And then, call addCustomError from your controller instead of addError