Hello everyone and thank you in advance
Have the form validation class where you get all form errors:
// Validação final do cadastro
public function validateFinalCadastro($arrVar) {
if(count($this->getErro())>0){
$arrResponse=[
"retorno"=>"erro",
"erros"=>$this->getErro()
];
}else{
$arrResponse=[
"retorno"=>"success",
"erros"=>null
];
}
return json_encode($arrResponse, JSON_UNESCAPED_UNICODE);
}
Here is the Controller that passes to view:
public function teste() {
//echo 'esse é um teste';
$this->recVariaveis();
$validate = new ClassValidate();
$validate->validateFields($_POST);
$validate->validateEmail($this->Email);
$validate->validateIssetEmail($this->Email);
$validate->validateData($this->DataNascimento);
$validate->validateCpf($this->Cpf);
$validate->validateConfSenha($this->Senha,$this->SenhaConf);
$validate->validateStrongSenha($this->Senha);
$validate->validateCaptcha($this->gRecaptchaResponse);
$validate->validateFinalCadastro($this->arrVar);
}
If I give an echo, print, or vardump in $ validate-> validateFinalCadastro ($ this-> arrVar); I can pass it to the view and present the error ... it shows after closing the html page ...
{"return": "error", "errors": ["Email already registered!", "Use a stronger password!", "Security system has been activated! Please refresh the page and try again or wait another one little. "]}
Here is the ajax without datatype: 'json'
No datatype: 'json' I enter the success reponse plus I fall into the else, because the response.retorno is without the datatype: 'json'.
$ ('# FormCadastroClientes') on ('submit', function (event) { event.preventDefault (); var data = $ (this) .serialize (); console.log (data);
$.ajax({
url: getRoot()+'CadastroClientes/teste',
type: 'post',
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
data: dados,
success: function (response) {
$('.retornoCad').empty();
if(response.retorno == 'erro'){
getCaptcha();
$.each(response.erros,function(key,value){
$('.retornoCad').append(value+'');
});
}else{
$('.retornoCad').append('Dados inseridos com sucesso!');
}
},
error: function (response, jqXHR, request, status, errorThrown, erro) {
console.log(response);
//alert(dados);
console.log(dados);
//alert(jqXHR);
console.log(jqXHR);
//alert(request);
console.log(request);
//alert(status);
console.log(status);
//alert(errorThrown);
console.log(errorThrown);
//alert(erro);
console.log(erro);
},
complete: function (jqXHR, textStatus) {
//colocar aqui algo que deseja que faça ao terminar todo o processo (finnaly)
}
});
Ajax with datatype: 'json' I can not get into success it already falls into error
$('#FormCadastroClientes').on('submit', function(event){
event.preventDefault();
var dados=$(this).serialize();
console.log(dados);
$.ajax({
url: getRoot()+'CadastroClientes/teste',
type: 'post',
dataType: 'json',
data: dados,
success: function (response) {
$('.retornoCad').empty();
if(response.retorno == 'erro'){
getCaptcha();
$.each(response.erros,function(key,value){
$('.retornoCad').append(value+'');
});
}else{
$('.retornoCad').append('Dados inseridos com sucesso!');
}
},
error: function (response, jqXHR, request, status, errorThrown, erro) {
console.log(response);
//alert(dados);
console.log(dados);
//alert(jqXHR);
console.log(jqXHR);
//alert(request);
console.log(request);
//alert(status);
console.log(status);
//alert(errorThrown);
console.log(errorThrown);
//alert(erro);
console.log(erro);
},
complete: function (jqXHR, textStatus) {
//colocar aqui algo que deseja que faça ao terminar todo o processo (finnaly)
}
});
Comments: - I can put everything in the bank. - I can introduce rertun json_encode directly to the view - I can enter the success of ajax without using datatype: 'json' does not enter in the if to read the array else in the else.
I get these errors in console.log
No response I would have to get the return from json_encode and I do not receive. What I get is the view html.
Ajax is expecting a return in json and not the html page.
parsererror SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
Thanks to all ..... atheist