Laravel 5.2表单验证回调函数与ajax

时间:2017-01-06 16:26:05

标签: ajax laravel-5.2

我正在尝试在表单验证中进行回调。 但我无法访问

  

$请求[ '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 ));
}

1 个答案:

答案 0 :(得分:3)

使用use()$request变量与您的查询一起插入。

 $validator->after(function($validator) use($request) {
        $beginningDate = $request['beginningDate'];