1

enter image description here

one email is required. and one checkbox should be checked in the first row. if the second email input is filled in the second row then there is also one checkbox should be checked and so on.

@for ($i = 0; $i < 4; $i++)
 <input type="email" id="email" name="email[]">
 <input type="checkbox" name="task[{{ $i }}][]" value="sign">
 <input type="checkbox" name="task[{{ $i }}][]" value="initial">
 <input type="checkbox" name="task[{{ $i }}][]" value="date">
 <input type="checkbox" name="task[{{ $i }}][]" value="cc"
 @endfor

this is the code in my controller its not catch the task field if it's null.

$validatedData = $request->validate([
            'email.0' => 'required_without_all:email.*|email',
            'email.1' => 'nullable|email',
            'email.2' => 'nullable|email',
            'email.3' => 'nullable|email',
            'task.*' => 'required_if:email.*,filled',
        ]);
7
  • Use required_with instead, required_if will check the value, which means your field will only be required when email is equal to 'filled', which will never be the case since it's not a valid email. Also you will have to store the values under the same array i think, like : contact.*.email and contact.*.task Commented Jan 17, 2023 at 8:07
  • @Lk77 you think ?? it means that you are not sure? Commented Jan 17, 2023 at 9:29
  • Yes it means exactly that, i'm sure at 90%, most likely it will not work if it's two different arrays, in the laravel doc the only supported syntax is : 'person.*.first_name' => 'required_with:person.*.last_name', see laravel.com/docs/9.x/validation#validating-nested-array-input Commented Jan 17, 2023 at 9:40
  • Have you validated on the front end or not? And what type of validation do you want? Commented Jan 17, 2023 at 10:05
  • @SulimanKhan I just want to validate it in my controller Commented Jan 17, 2023 at 10:07

3 Answers 3

2

I have searched a lot everywhere but the following code worked for me:

$request->validate([
            'email.*' => 'nullable|email',
            'email.0' => 'required|email',
            'task.0' => 'required_with:email.0',
            'task.1' => 'required_with:email.1',
            'task.2' => 'required_with:email.2',
            'task.3' => 'required_with:email.3',
]);
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this

First, change the field name like below

Here I used data as an array index in order to map the email, sign, date, and initial fields under the same index

@for ($i = 1; $i <= 4; $i++)
 <input type="email" id="email" name="data[{{ $i }}][email]">
 <input type="checkbox" name="data[{{ $i }}][sign]" value="sign">
 <input type="checkbox" name="data[{{ $i }}][initial]" value="initial">
 <input type="checkbox" name="data[{{ $i }}][date]" value="date">
 <input type="checkbox" name="data[{{ $i }}][cc]" value="cc"
 @endfor

Once you pass email in the first box and select the initial option and keep the rest unchanged and add dd($request->all()) in the controller you will output as given below.

array:2 [▼
      "data" => array:4 [▼
        1 => array:2 [▼
          "email" => "[email protected]"
          "initial" => "initial"
        ]
        2 => array:1 [▼
          "email" => null
        ]
        3 => array:1 [▼
          "email" => null
        ]
        4 => array:1 [▼
          "email" => null
        ]
      ]
    ]

Now add Laravel validation with the given below

$request->validate([
    'data' => 'required|array',
    'data.*.sign' => 'sometimes|required_with:data.*.email',
    'data.*.date' => 'sometimes|required_with:data.*.email',
    'data.*.cc' => 'sometimes|required_with:data.*.email',
    'data.*.initial' => 'required_with:data.*.email',
    'data.*.email' => 'required_with:data.*.sign|required_with:data.*.cc|required_with:data.*.date|required_with:data.*.initial',
 ]);

This will validate the form data both ways around. NOTE: You must add some custom validation to check if non of the values are selected.

I hope you find this helpful.

Comments

0
<?php 
class CourseLevelsRequest
    {
        private array $rules = [
            'course_levels' => 'required|array',
        ];

public function __construct()
{
    $this->makeValidationRules();

    request()->validate($this->rules);

    // your custom logic
}

private function makeValidationRules(): void
{
    collect(request('course_levels'))->each(function ($courseLevelRequest, $index) {
        $this->rules['course_levels.'.$index.'.course_id'] = 'required';
        $this->rules['course_levels.'.$index.'.level'] = 'required|string|max:255';
        $this->rules['course_levels.'.$index.'.programmatic_course_level_name']
            = 'required|string|max:255|unique:course_levels,programmatic_course_level_name,'.$courseLevelRequest['id'];
        $this->rules['course_levels.'.$index.'.fee'] = 'required|numeric';
    });
}

}

Here, I have full control of validation rules

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.