Return json of validations with laravel 5.4

i have the following request ajax:

$("#btnSave").click(function()
{ 
    var Url = 'http://localhost/Projetos-Laravel/Sibcomweb/public/panel/client/save';

    var Dados = $('#FormClient').serialize();

    $.ajax({
             type:'POST',
             url:Url,
             dataType: 'JSON',
             data: Dados,
             success:function(data){
                  if($.isEmptyObject(data.error)){
                     alert(data.msg);
                  }else{
                     alert('have errors');
                    }
                },
                error:function(e){
                  alert('Error !');
                  log.console(e);

                },
            });
        });

error log.console(e)

POST http://localhost/Projetos-Laravel/Sibcomweb/public/panel/client/save 500 (Internal Server Error)
send @ jquery.js:9566
ajax @ jquery.js:9173
(anonymous) @ create:431
dispatch @ jquery.js:5206
elemData.handle @ jquery.js:5014
create:446 Uncaught ReferenceError: log is not defined
    at Object.error (create:446)
    at fire (jquery.js:3317)
    at Object.fireWith [as rejectWith] (jquery.js:3447)
    at done (jquery.js:9274)
    at XMLHttpRequest.<anonymous> (jquery.js:9514)

the request ajax go to the controller but have some error in method of validation ...

I think the problem is in the validator method, am I doing something wrong?

public function store(Request $request)
{
    $dataForm = $request->all();

    $rules =[
    'name'=>'required|min:3|max:100',
    'number'=>'required|numeric',
    ];

    $valida = validator($dataForm, rules);
    if($valida->fails()) 
    {
        return $valida;
    }
    else
        return 'Ok';
}

how i do for return the var valida in type json?

Upvotes: 1

Views: 1333

Answers (3)

have two problems. first it was: log.console(e), but is console.log(e). and the second problem is in controller, the method of return not is correct.

that way it's correct:

public function store(Request $request)
{
    $dataForm = $request->all();

    $rules =[
    'name'=>'required|min:3|max:100',
    'number'=>'required|numeric',
    ];

    $valida = validator($dataForm, $rules);

    return response()->json(['error'=>$valida->errors()->all()]);
}

Upvotes: 0

Wim Pruiksma
Wim Pruiksma

Reputation: 598

log is not a fuction. Change log.console to console.log

Upvotes: 3

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

To see the response error:

Change your callback function on fail to:

  error: function(e){
    var errors = e.responseJSON;
    alert(errors);
    // Render the errors with js ...
  }

Upvotes: 0

Related Questions