1

I use the simple compare validation rule offered by Yii2 like this:

[confirm_email', 'compare', 'compareAttribute'=>'email', 'message'=>"Emails don't match"],

The problem is that this rule compares two emails 100% including Case Sensitive which means email@test.com and email@Test.com will generate validation error.

Is there a way to remove this Case Sensitive comparison from this rule?

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
AXheladini
  • 1,776
  • 6
  • 21
  • 42

2 Answers2

2

strcasecmp does not handle multibyte characters, read this

suggestion is to use strtolower()

you might also be interested in yii's input filter, to transform input to lowercase, like this:

[
    // both email fields tolower
    [['email', 'confirm_email'], 'filter', 'filter' => 'strtolower'],

    // normalize "phone" input
    ['phone', 'filter', 'filter' => function ($value) {
        // normalize phone input here
        return $value;
    }], ]
Community
  • 1
  • 1
e-frank
  • 739
  • 11
  • 21
0

You can create custom validation if you want.

public function rules()
{
    return [
        // an inline validator defined as the model method validateEmail()
        ['email', 'validateEmail'],
    ];
}

public function validateEmail($attribute, $params)
{
    if (strcasecmp($this->attribute, $this->confirm_email) == 0) {
         $this->addError($attribute, 'Username should only contain alphabets');
    }
}

It will compare emails with binary safe case-insensitive.

Mohan Rex
  • 1,674
  • 2
  • 17
  • 22