I am new to Yii framework & having some confusion regarding validation rules which are written inside model. For ex:
array(’email’,’safe’),
array(‘username, password’, ‘required’, ‘on’=>’login, register’),
1- what is the use of safe validator & where to use it?
2- what is the use of on scenario and how to use it? Suppose I have wriiten ‘on’=>’register’, then whether ‘register’ is action name or anything else.
Even after going through lots of document, still it is not very clear for me. Can someone explain it with example.
safevalidator can be used to indicate “this field can accept anything and no validation should be performed”. It’s relevant during mass-assignment:If the field has no rules set, its value will not be updated after this assignment. If a field does not require any validation, but should still be updated in this case, you can use the
saferule to indicate this (but be careful with that, validation is a good thing).In this case,
registeris a scenario (an arbitrary string of your choice, describing what is happening to the model). You assign the model’s scenario in the controller, before performing mass assignment and other work. When the time comes to validate the model, Yii will look at the model’sscenarioproperty and run the validators based on that. (In a reasonably sized project, you will want to use class constants instead of arbitrary strings for consistency.)Example: user password change. You can set up the following validator to mark the
passwordfield as required only in this scenario, and not otherwise:Look at this answer for some more information and links to further reading.