rules/validation: model is validate if attribute1 or attribute2 or attribute3 is save


I need to load data from the database to use them in an new/different kind (than before in my application).

In this case i load the model with ->findByPk()

The following code should only be executed if one of three attributes are not null/filled with an specific value.

First idea was to write some if-then-else construction.

Second idea was to use model validation and scenarios for that.

Is this an valid / smart use for Yii’s model validation?

If yes: how to use that? I didn’t get any ideas by reading that:

Thank you!

Can you give us an example of what you want please? pseudo code would do just fine if you need

Hi Asgaroth,

i need this for an news-website. Users can search into news. But before the application executes an search, we have to check, if there are enough serach-parameter available.

some pseudo-code as an example:

public function checkIfSearchIsUseful()

{$model = new myModel();  //myModel includes definitions for an search inside news-messages

if $model->PRIORITY has an value

 or $model->HEADLINE has an value

 or $model->SEARCHTERM hsa an value

->than return true //because you have minimum one information for what you can search

->else return false //the three main-attributes are empty - not enough information for an search.


Instead of the if construct maybe $model->validate is the better idea? (if it is possible)

Well other than else if cases I can only think of a custom validator combined with model scenarios:

That checks that at least one of your fields are required. if the form validates then do your search.

On an custom validator per default th system checks the validation of only on attribute (wich is passed trough by an parameter). That means, if i use something like that:


I thing, an simply if-than-else statement is the easiest with the smales overhead.