jeudi 14 mars 2019

laravel validate: checkbox requires another checkbox or input field in multidimensional array

I'm new to laravel and currently I struggle with the validation of an array with checkboxes.
Here is the foreach to show the table:

@foreach ($meeting as $meet)
 <tr>
  <td><input type="checkbox" name="meet[][room]" value=""></td>
  <td></td>
  <td><textarea name="meet[][day]" rows="1"></textarea></td>
  <td><input type="checkbox" name="meet[][tomorrow]" value="tomorrow"></td>
 </tr>
@endforeach

as you can see, there is a checkbox, in which room the meeting could take place. At least one of them has to be checked (no room for the meeting->no meeting).

When a room is checked, you have to choose whether the meeting is tomorrow(check the checkbox) or write down the day (the input field).

As it wouldn't make sense to check the checkbox for the meeting to be tomorrow and at the same time write it in the input field, you shouldn't be allowed to fill in both for one meeting.

The validation has to be for each row:

It's important that not every room checkbox has to be checked, but if so there are the rules below that have to be followed then.
If a room checkbox is checked, day must be filled in or tomorrow has to be checked, too (not both).
If room isn't checked, both day and tomorrow have to be empty/not checked or filled.

I first thought about something like this:

return [
  'meeting.*.room' => 'required_with:meeting.*.day,meeting.*.tomorrow',
  'meeting.*.day`=> 'required_with:meeting.*.room|required_unless:meeting.*.tomorrow,on',
  'meeting.*.tomorrow' => 'required_with:meeting.*.room|required_without:meeting.*.day'
]

I know about 'meeting.*.room', but when I tried something I had to check every room checkbox and fill in every day input field to get it working.
When I did this with just one row it wouldn't work, so I don't know how to do this.

I've tried it so many ways, even tried with closures but I'm somehow not getting what I need.

I hope anyone can help me with this.




Aucun commentaire:

Enregistrer un commentaire