skip to Main Content

Reading the docs, there doesn’t appear to be a way to validate the range of permissible values of numeric (non-integer) inputs. min and max validate the number of digits of the number, not the value. See my code:

$request->validate([
  'ratio' => 'required|numeric|min:0.01|max:1'; // not limited to 2 decimals
]);

Submitting the form with ratio=0.3333 fails:

The ratio field must not be greater than 1 character.

Is there any way to do what I want with built-in Laravel validation rules, or do I have to write my own?

2

Answers


  1. Chosen as BEST ANSWER

    Oh bother. My question was wrong.

    I simplified my code to as simple as I thought possible, but I'd over-simplified it.

    My actual code was

    $request->validate([
      'ratio' => 'required|numeric',
    ]);
    $request->validate([
      'ratio' => 'min:0.01|max:1',
    ]);
    

    which, it turns out, actually doesn't work.

    I added numeric the second rule, and then it worked.


  2. In my opinion, you need to validate this ratio between two values. So, I think this may be useful to you.

    'ratio' => 'required|numeric|between:1,100';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search