1

I'm trying to create validation for following request - data that is passed to given endpoint needs to be an array of arrays, where each inner array contains line1 and postcode and the size of the outer array is min:1. So, for example:

[
   ['line1' => 'foo', 'postcode' => 'bar'],
   ['line1' => 'baz', 'postcode' => 'qux']
]

is a valid data for my request and:

[
   ['line1' => 'foo', 'postcode' => 'bar'],
   ['line1' => 'baz']
]

is not.

I've created a Request validation class with following rules:

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

however I don't know how to add the min requirement. Neither '*' => 'min:1', nor '' => 'min:1' work (I'd think that first one should theoretically work, but I think it checks for length of each field to be 1)

0

1 Answer 1

1

Ideally you would add a key, i.e.

[ 'items' => [
       ['line1' => 'foo', 'postcode' => 'bar'],
       ['line1' => 'baz', 'postcode' => 'qux']
    ]
]

And then use 'items' => 'required|min:1'.

If this isn't possible, could you add an after() method to your Request (I haven't done this so not sure if it works):

public function withValidator($validator)
{
    $validator->after(function ($validator) {
        if (count($validator->getData())<1) {
            $validator->errors()->add('input', 'There must be at least one input');
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

The withValidator option worked for me. Thank you!

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.