0

I am building a small application in Laravel 5.6 where I am having an api which takes an array in format [1,2,5,90,25] I want to validate as required field in my validation rule.

I tried creating a request and validating the same as:

public function rules()
{
    return [
        'ProjectType.*'=>  'required',
    ]
}

public function messages()
{
    return [
        'projectType.*.required' => 'Project type is required',
    ];
}

But this thing is not working out, even if an empty array [] is being passed it accepts it.

How can we achieve these kind of array format

2

1 Answer 1

1

You must validate at the top level of the array, you may want this validation:

public function rules()
{
    return [
        'ProjectType'=>  'required|array',
        'ProjectType.*'=>  'required',
    ]
}

public function messages()
{
    return [
        'projectType.*.required' => 'Project type is required',
    ];
}
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.