Search code examples
phplaraveleloquent

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


Solution

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