skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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),
            ]);
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search