@ASDASDSA You can read this great article by Matt Stauffer https://mattstauffer.co/blog/form-array-validation-in-laravel-5-2
How to validate array input fields?
Hi!
Im working on kinda big form, where i insert a bunch of data from 10 fields and two radio buttons. The last four input fields are basic type="text" fields, and contains similar data. These fields should be stored in a separate table in the database. And I know I have done it before, to array the fields. But I'm having trouble with validations and errors.
I have copy'n pasted just two fields for the convenience. The fields looks like this:
<div class="form-group {{ $errors->has('choices[]') ? 'has-error' : '' }}">
<label class="control-label" for="choices[]">Svarsalternativ ett <span class="text-success">(rätt svar)</span></label>
<input class="form-control" id="choices[]" name="choices[]" type="text" value="{{old('choices[]')}}" placeholder="20 000" />
@if ($errors->has('choices[]'))
<span class="help-block">
<strong>{{ $errors->first('choices[]') }}</strong>
</span>
@endif
</div>
<div class="form-group {{ $errors->has('choices[]') ? 'has-error' : '' }}">
<label class="control-label" for="choices[]">Svarsalternativ två <span class="text-danger">(fel)</span></label>
<input class="form-control" id="choices[]" name="choices[]" type="text" value="{{old('choices[]')}}" placeholder="66 400" />
@if ($errors->has('choices[]'))
<span class="help-block">
<strong>{{ $errors->first('choices[]') }}</strong>
</span>
@endif
</div>
But how about the validation of array inputs? I have removed some of the fields for convenience and keeps the type_of_content that is a radio button, to detarmain what type of information that is sent thru.
$this->validate($request, [
'type_of_content' => 'present',
'choices[]' => 'required_unless:type_of_content,is_information',
]);
As you can see, I want to validate each input. But I can get it to work?
Thanks in advance.
ps. every key stroke I made while typing doing this thread made the screen scroll upwards, very enoying @JeffreyWay
ps2. great site otherwise
This is what i came up with
For the controller:
$this->validate($request, [
'choices.*' => 'required_unless:type_of_content,is_information',
]);
And for the view:
<div class="form-group {{ $errors->has('choices.0') ? 'has-error' : '' }}">
<label class="control-label" for="choices.0">Svarsalternativ ett <span class="text-success">(rätt svar)</span></label>
<input class="form-control" id="choices.0" name="choices[]" type="text" value="{{old('choices.0')}}" placeholder="20 000" />
@if ($errors->has('choices.0'))
<span class="help-block">
<strong>{{ $errors->first('choices.0') }}</strong>
</span>
@endif
</div>
<div class="form-group {{ $errors->has('choices.2') ? 'has-error' : '' }}">
<label class="control-label" for="choices.2">Svarsalternativ två <span class="text-danger">(fel)</span></label>
<input class="form-control" id="choices.2" name="choices[]" type="text" value="{{old('choices.2')}}" placeholder="66 400" />
@if ($errors->has('choices.2'))
<span class="help-block">
<strong>{{ $errors->first('choices.2') }}</strong>
</span>
@endif
</div>
Notice the choices.* in the validation and notice the (example) choices.1 in the form itself.
If you {{ dd($errors) }} in the view, you will get something like
#messages: array:4 [▼
"choices.0" => array:1 [▶]
"choices.1" => array:1 [▶]
"choices.2" => array:1 [▶]
"choices.3" => array:1 [▶]
]
Have a nice one!
Please or to participate in this conversation.