2

In Yii framework, how do you abort any further validation after one of rules returns FALSE ?

What I am trying to achieve is:

1) stopping unnecessary MySQL queries after we know that a model didn't pass the validation.

2) cleaner, easy to understand error messages for the web user, without sorting them manually in the controller.

Thank you in advance for your help!

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
Sebastian
  • 709
  • 1
  • 8
  • 15
  • are you trying validation with the validate() method? coz if you are, then i don't think there are any queries done by the validate method, not sure about it though. – bool.dev Feb 09 '12 at 03:19
  • 1
    I'm doing both the `validate()` and `save()` validations. Many validations are custom methods querying database, and some of the built-in ones (`unique`, `exists`) I believe do query MySQL, too. – Sebastian Feb 09 '12 at 03:25
  • i stand corrected, some obviously do queries. – bool.dev Feb 09 '12 at 03:33

2 Answers2

3

In short: there is no global setting or solution: If you look at CActiveRecord::validate(), you can see all validators are called and executed.

You can prevent running multiple validations for the same attribute. You would have to set skipOnError=true for all the validation rules.

http://www.yiiframework.com/doc/api/1.1/CValidator/#skipOnError-detail

whether this validation rule should be skipped when there is already a validation error for the current attribute. Defaults to false.

I think a global option could be added to Yii (quite easily actually).

marcovtwout
  • 5,230
  • 4
  • 36
  • 45
0

Thank you!

For users browsing this thread:

The validate() method - to be extended in your custom AR class - is located in yii/framework/base/CModel, line 150.

Sebastian
  • 709
  • 1
  • 8
  • 15