我正在使用Laravel Validator类对数组进行一些基本验证.
Am using the Laravel Validator class to do some basic validation on an array.
我的数组:
$employee['name']='name'; $employee['address']='address'; $employee['department']['department_name']='deptname'; $employee['department']['department_address']='deptaddress';我的验证规则如下:
$rules = array( 'name'=> 'required', 'address' => 'required', 'department.department_name' => 'sometimes|required' )以及以下自定义消息:
$messages = array( 'name.required' => 'Employee Name is required', 'address.required' => 'Address is required' 'department.department_name.required' => 'Department name is required' )我将使用Validator::make($employee, $rules, $messages);
根据我的规则,只有在数组中存在department_name时,才应验证department_name.但是,当前Validator存在且为空时,尚未验证department_name.有什么想法我可能做错了吗?
As per my rules, department_name should be validated if and only if it is present in the array. But currently the Validator is not validating department_name when its present and blank. Any ideas what I might be doing wrong?
推荐答案您在这里出错了,请参见文档此处
You are going a bit wrong here see the docs here
在那里提到如果我有$photos['profile'],那么我的验证将像这样
it is mentioned there If I have $photos['profile'] then my validation will go like this
$validator = Validator::make($request->all(), [ 'photos.profile' => 'required|image', ]);在上面的示例中,您的情况应该是这样
From the above example, In your case it should be like this
$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' )更多推荐
Laravel``有时''验证器因嵌套数组而失败
发布评论