Welcome Guest, Not a member yet? Register   Sign In
validacion json
#1

I am trying to send a JSON validation response but it only gives me the same result that I enter a value.

PHP Code:
$Moneda model('XXXX')->XXXX($datos);
        $this->validacion->setRules([
            'amount' => [
            'label'  => 'cantidad',
            'rules'  => 'required|decimal|greater_than_equal_to['.$Moneda['min_withdraw'].']|less_than_equal_to['.$Moneda['max_withdraw'].']',
            'errors' => [
                'required' => 'Ingrese la cantidad.',
                'greater_than_equal_to' => 'El campo de cantidad debe contener un número mayor o igual a 1000.',
                'less_than_equal_to'=> 'El campo de cantidad debe contener un número menor o igual a 10000.',
                ],
            ],
 ]);
        if ($this->validacion->withRequest($this->request)->run()) {
            
            
        
}else {
            $error $this->validacion->getErrors();
                echo json_encode($error);
                $this->response->setStatusCode(424);
        }


AJAX

$("#retiro").submit(function (e) { 
            var id = $(this).data('id');
            $.ajax({
                type"POST",
                url"/XXXX/XXXX/"+id,
                data"data",
                dataType"dataType",
                success: function (response) {
                    
                
},
                statusCode:{
                    424: function(xhr){
                    var json JSON.parse(xhr.responseText);
                        if (json.required.length !=0) {
                            alertify.set('notifier','position''top-right');
                            alertify.error(json.required);
                        }
                    }
                }
            });
            e.preventDefault();
            
        
});

RESULTADO:

Estado
424
Failed Dependency
VersiónHTTP
/1.1
Transferido675 B 
(tamaño 33 B)
Política de referenciastrict-origin-when-cross-origin

amount 
:"Ingrese la cantidad." 

If I enter data in the INPUT I get the same information 
Reply
#2

Setting the Output
Reply
#3

1. In your code you echoed the result instead of returning. Why is that ?  return $this->response->setJSON($data);

2. On the returned error i usually use the following loop. 

I'm usually working with ApiResponse trait so not sure if it will work for you. Just hope it will be helpful 
Code:
400: function(response) {
var response =  response.responseJSON.messages;                          
var output='';
for(var key in response) {
output+=response[key]+'<br>';                              
}
//Show output in html element here 
},
Reply




Theme © iAndrew 2016 - Forum software by © MyBB