The Laravel validator sometimes fails with nested arrays

We use the Laravel class Validatorto perform some basic validation in the array.

My array:

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

I have validation rules as shown below:

$rules = array(
    'name'=> 'required',
    'address' => 'required',
    'department.department_name' => 'sometimes|required'
)

And custom posts as below:

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

I'll use Validator::make($employee, $rules, $messages);

According to my rules, it department_nameshould be checked if and only if it is present in the array. But it Validatordoes not currently check department_namewhen it is present and empty. Any ideas what I can do wrong?

+4
source share
1 answer

, docs

$photos['profile'], :

$validator = Validator::make($request->all(), [
    'photos.profile' => 'required|image',
]);

.

$rules = array(
    'name'=> 'required',
    'address' => 'required',
    'employee.department.department_name' => 'sometimes|required'
)

$employee['department']['department_name']

, $message

$messages = array(
     'name.required' => 'Employee Name is required',
     'address.required' => 'Address is required'
     'employee.department.department_name.required' => 'Department name is required'
)
+1

Source: https://habr.com/ru/post/1535228/


All Articles