php - Laravel validator for nested array

390

Am using the Laravel Validator class to do some basic validation on an array.

My array :

$employee['name']='name';
$employee['address']='address';
$employee['department']['name']='deptname';
$employee['department']['address']='deptaddress';

I have the validation rules as below:

$rules = array(
    'name'=> 'required',
    'address' => 'required'
)

And the custom messages as below :

$messages = array(
     'name.required' => 'Employee Name is required',
     'address.required' => 'Address is required'
)

I will useValidator::make($employee, $rules, $messages);

How do I write the rule and messages for$employee[department]['name'] and$employee[department]['address'] using same rules and messages variables?

674

Answer

Solution:

Use dot notation for nested array:

department.name
department.address

People are also looking for solutions to the problem: javascript - Using JS OOP with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.