Back

Laravel 5.2 form validation call back function with ajax


nasik21 posted 9 months ago

I am trying to do a call back in form validation. But i can not access the

$request['beginningDate'] inside

$validator->after(function($validator) { it returns Undefined variable: 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'];
           
           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 ));
}

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us