0
I have a form that makes the registration of people. I need to send it with Ajax to not reload the page.
So far I have the following ready that I followed from a tutorial.
Js who makes the upload.
$("form#pessoasCadastrar" ).submit(function( event ) {
event.preventDefault();
data = $("form#pessoasCadastrar" ).serialize(),
url = '/pessoas/salvar',
var posting = $.post( url, { formData: data } );
posting.done(function( data ) {
if(data.fail) {
$.each(data.errors, function( index, value ) {
var errorDiv = '#'+index+'_error';
$(errorDiv).addClass('required');
$(errorDiv).empty().append(value);
});
$('#successMessage').empty();
}
if(data.success) {
$('.register').fadeOut(); //hiding Reg form
var successContent = '<div class="message"><h3>Registration Completed Successfully</h3><h4>Please Login With the Following Details</h4><div class="userDetails"><p><span>Email:</span>'+data.email+'</p><p><span>Password:********</span></p></div></div>';
$('#successMessage').html(successContent);
} //success
}); //done
});
Pessoarequest.php
public function rules(){ ... }
public function messages(){ .... }
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson())
{
return response()->json($errors, 422);
}
$dados = array('fail' => true, 'errors' => $errors);
return response()->json(array('fail' => true,'errors' => $errors));
}
Personal controller.php
public function salvar(PessoaRequest $request){
$p = new Pessoa();
[....]
return Response()->json(array('success' => true,$request->all()));
}
When submitting the form, if you do not fill in the input, it lands on a white screen with the Pessoarequest json return, stating which fields should be filled in. If successful when sending, gives the successful json return.
My problem is when the request error occurs, I would like to take the return json and treat in my view to show the errors, but whenever I send, it falls on this white screen with the errors
{"fail":true,"errors":{"nome":["O campo nome \u00e9 obrigat\u00f3rio!"]}}
How do I get this info in my view without reloading it?
I only managed to solve this problem still using Validator() instead of the request for requests with ajax
– Miguel Batista