6

I have the following naming conventions for input fields.

course_details[0][d_Total_Score]
course_details[1][d_Total_Score]
course_details[2][d_Total_Score]

Now I want to validate these fields with some rules. The following is the code I tried.

$validatedData = $request->validate([
    'course_details.0.d_Total_Score' => 'required',
    'course_details.1.d_Total_Score' => 'required',
    'course_details.2.d_Total_Score' => 'required'
]);

I have taken reference from here

But this doesn't seem to be working.

HTML CODE:

<input placeholder="SAT score " class="form-control form-control-sm valid" id="d_Score_Sub_Category_SAT" name="course_details[0][d_Total_Score]" value="" aria-invalid="false" type="text">

RESOLVED: As d3jn said, the validations should not override anywhere.

8
  • 1
    First of all, you can actually use wilcard syntax and let Laravel iterate through your corse_details with one rule: 'course_details.*.d_Total_Score' => 'required'. More about it here. Also provide code of where are you using this rules - in FormRequest class? Using Validator? Are there any exceptions? We need more info. Commented Jul 19, 2018 at 13:44
  • @d3jn, please check updates Commented Jul 19, 2018 at 13:47
  • Check dd($request->input('course_details')) when you send your requests. I assume "this doesn't seem to be working" means you are not getting any errors even when not specifying d_Total_Score. Or is it the opposite and you are always getting validation errors? Commented Jul 19, 2018 at 14:00
  • I am getting array ( 'd_Total_Score' => NULL, ), for example. But it is required. And the form is getting submitted. But it should not. Commented Jul 19, 2018 at 14:02
  • 1
    Based on what dd($request->input('course_details')) showed there might be other input with the name="course_details[d_Total_Score]" somewhere after your original inputs and it's overriding the value that is sent with the request. Commented Jul 19, 2018 at 15:03

1 Answer 1

3

You should be able to achieve what you are looking for like this:

course_details.*.d_Total_Score' => 'required'

I recently wrote something similar and I did it like this:

'contacts.*.name' => 'nullable|string|max:255',
'contacts.*.email' => 'nullable|email|max:255',
'contacts.*.phone' => 'nullable|phone:US|max:255',

My HTML looks like this:

<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[0][name]" type="text" value="">
<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[0][email]" type="text" value="">
<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[0][phone]" type="text" value="">

<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[1][name]" type="text" value="">
<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[1][email]" type="text" value="">
<input class="form-control{{ $errors->has('contacts') ? ' is-invalid' : '' }}" id="contacts" name="contacts[2][name]" type="text" value="">

... so on

Sign up to request clarification or add additional context in comments.

2 Comments

When you say it doesn't work, do you mean it's passing the validation or are you seeing errors? @DushyantJoshi
it is passing the validation. Means is is not being called.

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.