Home > Net >  Laravel validation is case sensitive in ends_with
Laravel validation is case sensitive in ends_with

Time:10-12

It looks small problem but I can't find the best solution. In Laravel validation, I need to check if the user enters the company email and I'm using ends_with check. But it is case sensitive

$request->validate([
                'username' => [...],
                'email' => ['required', 'string', 'email', 'max:50', 'unique:users', 'ends_with:mycompany.com'],
              
            ]);

So if the user enters mycompany.com it will pass the validation but fails when entering MYCOMPANY.COM or MYCompany.com

CodePudding user response:

You can use PrepareForValidation method in form request. First make the form request by : PHP artisan make request, then add this method to class :

protected function prepareForValidation()
  {
     $this->merge([
            'email' => strtolower($this->email),
        ]);
  }

CodePudding user response:

If you create a separate request, using artisan make:request, you can format the data before it goes through the validation with the prepareForValidation method

You can read more on that here https://laravel.com/docs/9.x/validation#form-request-validation

  • Related