clarification of validation rules

I am looking for some clarification of validation rules.

My assumptions as I believe I understand things. I would appreciate if someone could tell me if I am right or wrong.

‘safe’:

  • Assign to all hidden values in a form

  • Do not assign to PK

  • Only applies during a massive assignment

  • ‘on’=>‘search’ used for every value that a search may be performed on

‘unsafe’:

  • Can be used effectively in Scenarios to reverse a safe assignment

  • Only applies during a massive assignment

Now when it comes to setOnEmpty

I really don’t understand what the statement below means. Hence I really do not understand which attributes should be listed within it. Maybe someone can explain it different than the definition above?


array('', 'default', 'setOnEmpty' => true, 'value' => null),

The API reference states that setOnEmpty means unconditional assignment of the value.

Edit: sorry, should have been setOnEmpty=false

/Tommy

Thanks, that is a more complete definition there…

I know that this is pre-populated during model generation but it does not seem to be assigning it to every db attribute that is assigned a default value. Shouldn’t it be though?

Have a look at the source code here.

IIRC, in case of validation errors, the ordering of validation rules can have impact.

/Tommy


array('', 'default', 'setOnEmpty' => true, 'value' => null),

means nothing , and also does not give error .

does not set any variable as default if you pass in array … … .

if anybody find its use please tell.

I assume the author of this thread meant to specify a list of attributes in the first element of the rule.

Anyway, at least according to the API reference, there’s no support for an attribute wildcard.

/Tommy

Yes sorry there are attributes there, I thought I mentioned that in the original question as they are automatically generated with the model creation. Sure enough I didn’t mention it though.

Does anyone have feedback in regards to safe/unsafe?

All ok, with notes below.

‘safe’:

  • Assign to all hidden values in a form [also to attributes that should be massively assigned and don’t have other rules]

  • Do not assign to PK [correct for autoincrementing ones. But you may want a PK to be assigned if it is not autoincrementing. Note: better to have a validation rule for the PK.]

  • Only applies during a massive assignment

  • ‘on’=>‘search’ used for every value that a search may be performed on

‘unsafe’:

  • Can be used effectively in Scenarios to reverse a safe assignment [or other validation rule]

  • Only applies during a massive assignment


array('', 'default', 'setOnEmpty' => true, 'value' => null),

Means: When the specified parameters are empty (see setOnEmpty), they will get the default value (see value).

Useful to avoid inserting empty strings to the database (and insert null instead).

Thanks mentel for the clarification. It now makes sense.

Just want to update this to maybe save someone some time.

The order of the validation rules matters. if you are using filter make sure it comes before or you will not get null inserted.


 array('','filter', 'filter'=>'trim'),

array('', 'default', 'setOnEmpty' => true, 'value' => null),