0

How do I validate the values in an array from a request? I have the following rules

 public function rules()
    {
        return [
            'firstname.*'  => ['required', 'string', 'max:45'],
            'surname.*'  => ['required', 'string', 'max:45'],
            'email.*'  => ['required', 'string', 'email', 'max:45'],
            'phonenumber.*'  => ['required', 'string', 'max:45'],
            'name' => ['required', 'string', 'max:45', 'unique:organisations'],
            'info' => ['required', 'string'],
            'address' => ['required', 'string', 'max:45'],
            'housenumber' => ['required', 'string', 'max:10'],
            'postalcode' => ['required', 'string', 'max:7'],
            'place' => ['required', 'string', 'max:45'],
            'password' => ['required', 'confirmed']

        ];

firstname, surname, email and phonenumber are arrays with the following input

<input name="firstname[]">
<input name="surname[]">
<input name="email[]">
<input name="phonenumber[]">

The other validation field work just fine. I tried the answer from this post: How to validate array in Laravel?

however it doesn't work for me. What am I missing or doing wrong?

2
  • Whats the error it gives? Everything seems OK to me. Btw, are you applying the custom request to the function in the controller? Commented Mar 17, 2020 at 17:51
  • I am not getting any errors. The array fields simply are not validated Commented Mar 17, 2020 at 17:53

1 Answer 1

0

oke, the validation was working, but I had to access my array with a dot followed by the index instead of using the traditional [index] to access array elements. So the input now looks like this:

input id="firstname" type="text" class="form-control @error('firstname.0') is invalid @enderror" name="firstname[]" value="{{ old('firstname.0') }}" autofocus>

    @error('firstname.0')
      <span class="invalid-feedback" role="alert">
         <strong>{{ $message }}</strong>
      </span>
    @enderror
Sign up to request clarification or add additional context in comments.

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.