jeudi 14 mars 2019

laravel: checkbox requires other checkbox or textarea to be checked/filled

I have a problem with laravel validation which I am unable to solve.

Here is a foreach that gives a table:

@foreach ($meeting as $meet)
 <tr>
   <td><input type="checkbox" name="meet[][person]" 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

Each meeting (table row) includes some persons(chosen with the checkbox) and can be at a specified day (textarea) or tomorrow(another checkbox).

At least one person has to be chosen and for this person, it's important to fill in the day textarea or check the tomorrow checkbox (It has to be done for every person though).

When a person isn't checked, day and tomorrow have to be empty.
When day is filled with data it requires the person in this row to be checked, the same when tomorrow is checked.

I am somehow unable to do this.
I have tried this for example since it made sense for me:

'meeting.*.person' => 'sometimes|required_with:meeting.*.day,meeting.*.tomorrow',
'meeting.*.day' => 'required_if:person,on|required_unless:tomorrow,on',
'meeting.*.tomorrow' => 'required_if:person,on|required_without:day',

When I try this code it won't accept that not every person is checked and it won't accept the value in day ..
I think I need custom validation rules or something like that, but I don't know how to do them for this case - especially with the foreach.

I would really appreciate if someone could help me with that.




Aucun commentaire:

Enregistrer un commentaire