Ad

Laravel Request Validation Skips Array Value

- 1 answer

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?

Ad

Answer

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
Ad
source: stackoverflow.com
Ad