问题
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.
回答1:
A small update if this doesn't work.
Try:
'count' => 'required|numeric|max:{$available}'
回答2:
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.
来源:https://stackoverflow.com/questions/31703964/laravel-validation-rule-maxvalue-not-working-with-variables