Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 832 Bytes

Ajax Validation.md

File metadata and controls

34 lines (24 loc) · 832 Bytes

Backend validation part

<?php

// when form is posted here via AJAX
function action_in_controller(Request $request){

    try {

        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'description' => 'required',
            'price' => 'required',
            'price_btc' => 'required',
            'file_resource' => 'required'
        ]);

        if ($validator->fails()) {
            // return all validation errors as array
            return response()->json(['status' => 'error', 'error' => $validator->errors(), 'point' => 'validation failed']);
        }

    } catch (\Exception $e) {

        // unknown exception, pass message as array
        return response()->json(['status' => 'error', 'error' => [$e->getMessage()], 'point' => 'exception']);

    }

}

?>