nexxai
338
2
Testing

Testing validation of an input array

Posted 10 months ago by nexxai

Let's say I have this HTML:

<input type="text" id="name" name="name[]" class="form-control" value="" required="">
<input type="text" id="name" name="name[]" class="form-control" value="" required="">
<input type="text" id="name" name="name[]" class="form-control" value="" required="">
<input type="text" id="name" name="name[]" class="form-control" value="" required="">

And in my request validator, I have this:

public function rules()
{
    return [
        'name.*' => 'required|string'
    ];
}

The test that I'm trying to write to ensure that the validation is working is:

    $person = make('App\Person', ['name' => null]);
    $this->post('/person', $person->toArray())
        ->assertSessionHasErrors('name');

The validation actually works and makes sure that each entry has data in the field, but I don't understand how I'd test that with assertSessionHasErrors, because it looks for the name of the variable, but in this case it's an array ("name[]") rather than a single variable (e.g. "name").

I've tried variations on the assertSessionHasErrors() like:

        ->assertSessionHasErrors('name[]');

        ->assertSessionHasErrors('name.*');

But nothing I try will make it work.

Does anyone know what I'm missing here?

Please sign in or create an account to participate in this conversation.