23

I know that you can have a validation rule that applies only for one scenario:

array('username', 'exist', 'on' => 'update'),

Now i would like to know if it's possible to do the opposite: a rule that applies everytime except for a given scenrio?

The only solution that is see right now is list all the others scenario, but it's not pretty if we need to add some news scenarios later.

array('username', 'exist', 'on' => array('create', 'search', ...),//all the scenarios except update
Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
darkheir
  • 8,844
  • 6
  • 45
  • 66

2 Answers2

38

As of Yii 1.1.11 you can use except keyword:

array('username', 'exist', 'except' => 'update'),

Take a look at this page. There is a little example there.

Doc link

bool.dev
  • 17,508
  • 5
  • 69
  • 93
rinat.io
  • 3,168
  • 2
  • 21
  • 25
  • Really good, I should have found it by myself since it's in the documentation (but not in the wikis about validation rules)! Thanks! – darkheir Nov 08 '12 at 07:35
1

Work the same way in Yii 2.0.

['username', 'required', 'except' => 'update']

Every key in the array before the name of the validator is a property for the Validator Class itself. You can see the available properties in https://www.yiiframework.com/doc/api/2.0/yii-validators-validator

I know is an old question but every time I forgot that yii2 have an except property in the validator class.

https://www.yiiframework.com/doc/guide/2.0/en/input-validation for more advanced technics