String length validation

Hi, I have this situation:

in validation rules, I have a rule:

[['access_key'], 'string','length'=>32],

I fill the value of access_key in beforeSave method.

I’ve looked through yii2 source code - the validate() runs before beforeSave().

The question is: why don’t I get error when I do not set access_key ?


Have you specified a ‘required’ rule?

[['access_key'], 'required'],

No, I didn’t. Does this means that in the absense of ‘required’ null or converted to false values are not validated?

Doesn’t length there mean max length and not minimum (sorry, haven’t used Yii 2 yet)?

I found the answer in the manual:

It means empty input validation is skipped in my case.

Yes, but you can always configure the skipOnEmpty property,

[[‘access_key’], ‘string’, ‘length’ => 32, ‘skipOnEmpty’ => false],