8

I am using Yii2 and attempting to implement a rule on my usernames so that they are always stored and compared in lowercase. If there a rule that I can use to make this possible?

For instance, I have a function that checks if username exists in the database. I want to avoid logic errors and implement a global rule if possible. Thanks for any tips!

Yii2 Rules:

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password'], 'required'],
            [['access_level'], 'integer'],
            [['username'], 'string', 'max' => 50], // force lowercase?
            [['username_print'], 'string', 'max' => 50],
            [['password'], 'string', 'max' => 512],
            [['email'], 'string', 'max' => 250],
            [['username'], 'unique']
        ];
    }
BajaBob
  • 2,643
  • 3
  • 24
  • 26

1 Answers1

14

Use this way

['username', 'filter', 'filter'=>'strtolower'],
SO-user
  • 1,458
  • 2
  • 21
  • 43
  • Tested this for adding users to the table and searching for users in the table and each time the system forced a lowercase operation as expected. Thank you! – BajaBob Feb 21 '15 at 14:49