laravel validation rule max:value not working with variables

让人想犯罪 __ 提交于 2020-01-06 18:32:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!