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,338
  • 9
  • 45
  • 76
AXheladini
  • 1,697
  • 6
  • 20
  • 38

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
  • 649
  • 12
  • 20
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,664
  • 2
  • 16
  • 22