Amr Emara
Amr Emara

Reputation: 21

laravel validation rule max:value not working with variables

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.

Upvotes: 1

Views: 5422

Answers (3)

John Perczyk
John Perczyk

Reputation: 11

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.

Upvotes: 1

Marcus Christiansen
Marcus Christiansen

Reputation: 3207

A small update if this doesn't work.

Try:

'count' => 'required|numeric|max:{$available}'

Upvotes: 1

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40919

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.

Upvotes: 0

Related Questions