Home > Software engineering >  Laravel 8 checkbox validation that count checked item more than certain value from another field
Laravel 8 checkbox validation that count checked item more than certain value from another field

Time:02-17

i have this blade view

<input type="text" value="{{$borongan->worker}}" id="worker" name="worker">
@foreach ($employees as $employee)
<input id="boronganWorker[]" type="checkbox" name="boronganWorker[]" value="{{$employee->empid}}"> {{$employee->nama}}<br/>
@endforeach

i need to validate using Laravel validation, that the count of checked item in boronganWorker is gte than the worker value.

already tried these two,

$request->validate(
[
'boronganWorker' => 'required|gte:worker'
]);
\\or
$request->validate(
[
'boronganWorker' => 'required|min:worker'
]);

but still cant get the right result.

For example if worker variable have value=5 :

  • if checked less than 5 --> error message.
  • if checked gte 5 --> pass.

Please help me to find answer. Thanks in advance

CodePudding user response:

Please use * with names. Because when you want to validate array you have to use *.

ex.

$request->validate(
[
'boronganWorker.*' => 'required'
]);

CodePudding user response:

So, this is how i solved this problem.

Need to keep the limit on variable, and then use it in the validation using double quotes

$limit=$request->worker;
$request->validate([
'boronganWorker' => ['required','array',"min:$limit"]
]);

i get the answer from laracast, i dunno if it allowed to share it here, but here it from link

  • Related