confuse in rules()

Hi, I just want to ask in rules() found in user.php,


    const STATUS_ACTIVE = 10;

    const ROLE_USER = 10;

public function rules()


        return [

            ['status', 'default', 'value' => self::STATUS_ACTIVE],

            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

            ['role', 'default', 'value' => self::ROLE_USER],

            ['role', 'in', 'range' => [self::ROLE_USER]],



I tried to change the role to 1 but I can still login what is the purpose of role in range=>self::ROLE_USER

Thank you in advance.

You have to create your own logic to manage the value of role. Yii 2 is providing a nice example of data sturcture and how you can set the default value for it, but it’s up to you to take it from there.

One thing to not confuse. On behaviors in controller, if you are using access control, you will see under rules:

'roles' => ['@'],

This has nothing to do with the role field in your user table.

Yii 2 also comes with authManager component and RBAC, which are fully featured and advanced topics, you can find them in the guide here. Depending on the scope of your project, you might not need to go that deep.


I just created a post and a tutorial for implementing a simple RBAC for the advanced template in 6 easy steps. Try it out and see if it works for you.

Thank you so much :)