Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter to force lowercase on all operations

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']
        ];
    }
like image 840
BajaBob Avatar asked Feb 20 '15 22:02

BajaBob


1 Answers

Use this way

['username', 'filter', 'filter'=>'strtolower'],
like image 108
SO-user Avatar answered Sep 21 '22 05:09

SO-user