3

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 use Validator::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?

1 Answer 1

8

Use dot notation for nested array:

department.name
department.address
Sign up to request clarification or add additional context in comments.

6 Comments

May be you can surely help me with this stackoverflow.com/questions/22892058/…. 'sometimes' validator is not working with nested arrays.
Well, currently it's impossible to do, but I'm gonna make a pull request to fix this issue. Only then will you be able to make it work.
@JarekTkaczyk any idea how to do it in Laravel 4?
Given the answer was given in April 2014 it should work in L4 ;)
What if we are having arrays in department. like department[0]['name']; department[0]['address']; department[1]['name']; department[1]['address'];
|

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.