Muhammad
Muhammad

Reputation: 634

Laravel: Do validation upon two separate conditions

I have a table in my html form, where the user is free to add as many rows as they would like. Underneath the table I have a checkbox, which when checked indicates the user has no inputs.

I have a bunch of inputs in Laravel which are in an array (so it can name[0] and name [1] or just name[0] depending on how many rows the user added). I also have a checkbox, which makes all my inputs disappear, and I wouldn't like to make any field required if the user checked it.

My current rules are:

[
        'TB1_course.*' => 'required_if:TB1_checkbox, 0|required_with:TB1_section.*,TB1_session.*,TB1_reqElec.*',
        'TB1_section.*' => 'required_if:TB1_checkbox, 0|alpha_num',
        'TB1_session.*' => 'required_if:TB1_checkbox, 0'
]

My validation rules required:

(1) If the user selects the checkbox, I would like to make no field in the entire table required (regardless if those inputs are empty or not)

(2) If the user didn't select the checkbox and left one row entirely blank (i.e. TB1_course[0], TB1_section[0], and TB_session[0] empty OR supplied TB1_course[0], TB1_section[0], TB1_session[0] but left TB1_course[1], TB1_section[1] and TB1_session[1] empty), inputs are valid.

(3) If the user didn't select the checbox and also gave missing info on the same row (i.e. TB1_course[0] isn't empty but TB1_section[0] and TB1_session[0] are empty), make it invalid.

I am unsure how to go about this, since I also have to worry about indices. I'm a beginner in validation, so please excuse me if I am asking a very basic problem. Any help is greatly appreciated.

Upvotes: 0

Views: 168

Answers (1)

user320487
user320487

Reputation:

Your inputs should have the required_without:checkbox rule applied to them, while the checkbox should have the required_without_all:input1,input2,... rule applied to it. Then you can use the required_unless rule on the inputs to check the validity per your guidelines you laid out above.

Upvotes: 1

Related Questions