Klapsius
Klapsius

Reputation: 3359

Laravel validation is case sensitive in ends_with

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

Upvotes: 2

Views: 1147

Answers (2)

parastoo
parastoo

Reputation: 2469

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),
        ]);
  }

Upvotes: 6

Oskar Mikael
Oskar Mikael

Reputation: 326

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

Upvotes: 1

Related Questions