Search code examples
laravellaravel-5.8laravel-validation

How to validate inputs from GET method? laravel


How can I validate my inputs from a GET method?

Example URL: localhost:8000?salary=2000&name=sample&description=vowewljfodigjfdglfd

In the URL I have 3 inputs and I want to validate:

  • Salary - should accept only numeric
  • Name - should accept only alphabetic
  • Description - should accept with max:1000

Somebody knows how to do this?


Solution

  • The Laravel validator doesn't care where the data came from. You can manually create a validator and pass it the query string data.

    $validator = Validator::make($request->query(), [
        'salary' => 'numeric',
        'name' => 'alpha_num',
        'description' => 'max:1000',
    ]);
    
    if ($validator->fails()) {
        // show an error
    }
    

    Side note: As someone with a hyphenated last name, I implore you not to treat name as alphanumeric. See Falsehoods Programmers Believe About Names.