Passing data to create view from store method in Laravel

240 Views Asked by At

I'm using a jquery repeater plugin and I need to post a json data which is repeater fields data to store method and back that data if there are validation errors. How that is possible?

1

There are 1 best solutions below

1
On

Use laravel builtin validations and it will return you back with errors (if any).

public function store(Request $request){
    $validator  = Validator::make($request,[
       'name' => 'required',
       //...
     ]);
   if($validator->fails()){
   return response()->json(['errors' => $validator->errors()]);
   }
} 

jQuery Side validate input on form submit

var name = $('#name').val();
if(name.length < -1){
   alert('name is required');
}