0

I send an array to a REST API. How can I add a rule for the array?

Also I want to add field_name_id, field_input_type and field_caption as required fields.

I don't know how can I access the array in Laravel rules. Can someone help me?

$rules = [
    'name'  => 'required',
    'forms' => 'array'
]

enter image description here

2
  • Are you sending the array of objects in JSON format to the Laravel REST API ? Commented Oct 25, 2017 at 14:00
  • @Pratik no i send an objects of array format. That screenshot from response, i response inputs to share with you. Commented Oct 26, 2017 at 8:27

1 Answer 1

2

Laravel uses dot notation to validate arrays and it's nested fields.

$rules = [
    'forms.field_name_id' => 'required', 
    'forms.field_input_type'=> 'required', 
    'forms.field_caption' => 'required',
]

You can also validate each value within the array. For example, If you want the caption to be unique:

$rules = [
    'forms.*.field_caption' => 'unique:captions,caption',
]

Here are the docs for more information on how to use them

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.