1

i’m a newby in laravel , i’m trying to validate a certain field from my input request using the max:value rule , the thing is that the value is neither a static value nor a field in my request input , its a value that i retrieve from my database and stored in a variable $available , whenever i try ( ‘count’ => required|numeric|max:$available ) the validation always fails on max as if it cannot evaluate that variable $available , however when i try to use a static value which is not the case (e.g ‘count’ => required|numeric|max:5 ) it works just fine , i also tried to merge $available with the input fields of my request before validation using “ $request->merge(['availableCount' =>$availableRoom->count]); “ and then i tried to use the field ‘availableCount’ in my rule instead of the variable (e.g ‘count’ => required|numeric|max:availableCount) the validation never fails and it always proceeds with the controller logic !! what did i do wrong or what am i supposed to do ?! Thanks.

Amr Emara
  • 21
  • 1
  • 2

3 Answers3

1

A small update if this doesn't work.

Try:

'count' => 'required|numeric|max:{$available}'
Marcus Christiansen
  • 3,017
  • 7
  • 49
  • 86
0

Use

‘count’ => "required|numeric|max:$available"

instead of

‘count’ => 'required|numeric|max:$available'

When you use single quotes then $available is not evaluated so what you pass to validator is literally max:$available.

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
0

A bit late but just in case anyone is needing this

In Laravel 8 this works:

'count' => "required|numeric|max:$available"

Use Double Quotes instead of single.

  • the validation error will be the field is more than $available .. which is not very good for UX – mercury Mar 31 '22 at 22:59