addError() parameter question

Hey everyone,

I am wondering: does the $attribute param of the addError() function of an activeRecord model need to be a valid attribute of the model or can Yii kinda just get the error message and place it in a summary?

Basically I have a large form with many sub models being validated all at the same time. To handle these sub-models I basically add a bit of validation and a tester at the end like so:




if(!$is_GeozoneClassificationValid)

	$model->addError('Please double check you have completed the Geozone classifications correctly.');



Thing is that this error message will be shown in a summary and not to a particular attribute so I am wondering if there is some kind global $message array that I can hook into to add general errors? Does Yii allow me to put dummy attributes into the first param the of the addError() function and it will just show the message in the summary?

Thanks,

There is no meaning in adding generic "errors" to the model.

Models and Active records consist of attributes that we manipulate with.

So you always must bound error message to a particular attribute like so.

(inside model/AR class)




$this->addError('attribute', 'You did something wrong...')



You can show summary of all errors e.g.:

(inside of view file)




MyModel::model()->getErrors();



Or do you have any example where this would be meaningful?

So far, I have never needed "generic" model error…

Lubos

Hi thanks,

A generic error would be good where, for example:

I am adding a book to a cataloguing system that has 3 sub models.

Instead of having to manage the errors of all of them I just add a generic error to the parent model "Title" for each sub model that does not validate:

"You did something wrong in x model"

As you said I bound these errors for the min to on the fields within the model and just hide that fields inline errors and show all errors in a summary.

Thanks,

Hi,

well, Yii does not implement any “submodel” behaviours. It’s always just a model with attributes you work with.

I think that you are talking about something like this:




ParentCatalogue extends CActiveRecord{...}


ChildrenCatalogue1 extends ParentCatalogue{...}


ChildrenCatalogue2 extends ParentCatalogue{...}



If so, you can easily implement shared ActiveRecord with your own "generic" errors:




ActiveRecord extends CActiveRecord{

   public $genericErrors = array()


   public function addGenericError($message){

     $this->genericErrors[] = $message;

   }

}


ParentCatalogue extends ActiveRecord{...}


ChildrenCatalogue1 extends ParentCatalogue{...}



It is pretty easy to add such a support, but answer to your original question is: NO, Yii does not support generic model errors. Neither does Zend_Model from Zend Framework.

Lubos

Thanks,

Your children class version gave me an awesome idea of how to standardise these errors across the classes :).

Didn’t use it exactly since I was meddling with title, geozone and subject Models but I was able to make a small layer under CActiveRecord to do just this. The layer was needed anyway our system was starting to get some serious generic functions per model.