我正在尝试在表单验证中进行回调。 但我无法访问
$请求[ 'beginningDate']
内
$ validator-> after(function($ validator){
返回未定义变量:request
function store(Request $request){
$validator = Validator::make($request->all(),[
'beginningDate' => 'required|date|before:endingDate',
'endingDate' => 'required|date|after:beginningDate',
]);
$validator->after(function($validator) {
$beginningDate = $request['beginningDate'];
$endingDate = $request['endingDate'];
$beginningDate = 'a';
if ($beginningDate != '2015-02-01') {
$validator->errors()->add('fendingDateield', 'Something is wrong with this field!');
}
});
if ($validator->fails()) {
return [
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
];
}
echo json_encode(array( 'success' => true, 'errors' => null ));
}
答案 0 :(得分:3)
使用use()
将$request
变量与您的查询一起插入。
$validator->after(function($validator) use($request) {
$beginningDate = $request['beginningDate'];