addError problem

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());


When you call validate(), it will clear all existing error messages.

You should mainly use addError() in a validator.

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!

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.');

}

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 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.

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