1

My request looks a bit like this:

"adults": {
    "count": 2,
    "persons": {
        [
            "name": "Anne",
            "surname": "Doe",

        ],    
        [
            "name": "Anne",
            "surname": "Doe",

        ],
    }
}

How can I easily verify using a custom Request that the count field matches the size of the persons?

Here's my Rules:

class CreateFamilyRequest extends ApiRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'adults' => ['array'],
            'adults.number' => ['int'],
            'adults.persons.*.name' => ['string'],
            'adults.persons.*.surname' => ['string'],
        ];
    }
}


1 Answer 1

1

You are in context of the request, so you can access it as you would in the controller, by using $this for current object context. Therefor you can just fetch out the size, assuming this is a JSON request, else change the input method.

'adults.persons' => ['array', 'size:' . $this->json('adults')['count'] ?? -1],
Sign up to request clarification or add additional context in comments.

1 Comment

Both the validation key and the json access was wrong updated my answer.. Still the idea remain the same

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.