Better model scenario management

I already found this troubling on a few larger projects, but this time I decided to find "smarter" solution.

Thing is that when you create model from yiic, you have predefined validation rules that apply to every scenario. Process is that you add or remove validation rules for specific scenarios and that is OK when you have 5-6 scenarios.

Things get complicated when you have more than a few different scenarios and you pretty much end up with large list of scenarios for every rule. Also, process to make that list is not trivial specially when you add new scenario, assuming that you’ll test all previous scenarios to check if you not messed something up. Not mentioning that it’s confusing and difficult to maintain those scenario lists after some time. If you don’t know what I mean, check examples below…

I came up with two solutions…

IMHO, first is with "notOn" parameter, which would act as opposite to "on" parameter. For example if you have this rule :




array('firstName, lastName', 'required','on'=>array('insert','update','and_lots_of_more_scenarios_here'))



that apply to all of your scenarios except (for example) ‘passwordReset’.

This could be replaced with :




array('firstName, lastName', 'required','notOn'=>array('passwordReset'))



Other way (and maybe better) would be to have possibility to overwrite validation rules if you want to (only with specific validation type). Then, adding new validation type (for example none) would avoid validation for that attribute. Same example would be like this:




array('firstName, lastName', 'required')), //for all scenarios

array('firstName, lastName', 'none','on'=>array('passwordReset')) //only for passwordReset scenario



Since ‘none’ validation rule is “newer”, it would overwrite previous ‘required’ rule. This, of course wouldn’t apply to other validation rules and you could “stack” rules as you did before.

I have the same problem and would really like the ‘notOn’ option to basically exclude the validation rule for a given scenario.

Just ran into the same problem myself, any news on this? Placed as a feature request?

Just found this back from 2009, makes a good point.

http://code.google.com/p/yii/issues/detail?id=576