Ragnar Ragnar - 1 month ago 9
Ajax Question

Codeigniter not displaying validation_errors() on ajax call in form validation

I don't understand why i'm geting no errors when I left empty inputs on my form. I'm using form validation at first time, and I'm really confuse about how it's the way to submit a form validation with ajax. I'm using a file called form_validation.php on my application/config path as explains the codeigniter web page, to set the rules validation of every input in my app. I already set the form helper and form_validation library on autoload.php. On form view I try to print errors through validation_errors(), but nothing happens... so here is my code, HELP PLS!!

FORM

<?php echo form_open(null,array("class"=>"col s12","id"=>"enter_form"));?>

<div class="row">
<div class="col s4">
<span style="position:static;" class="badge yellow">Importante!!!</span>
<blockquote>
Si es primera vez que ingresas a votar, debes ingresar un correo y una contraseƱa, que te identifique como usuario.
Si ya te encuentras registrado, ingresa los mismos datos usados la primera vez que ingresaste, para realizar tu inicio de sesion.
</blockquote>

</div>
<div class="col s8">
<?php

$errors= validation_errors('<li>','</li>');
if($errors != ""){
?>
<div class="red lighten-2">
<ul>
<?php echo $errors;?>
</ul>
</div>
<?php
}

?>
<div class="row">
<div class="input-field col s6 offset-s3">
<input id="enter_email" type="email" name="enter_email" class="validate">
<label for="enter_email">Correo</label>
</div>
</div>
<div class="row">
<div class="input-field col s6 offset-s3">
<input id="u" name="enter_password" type="password" class="validate">
<label for="enter_password">ContraseƱa</label>
</div>
</div>

<input type="hidden" name="update_politic_hide" id="update_politic_hdn" value="">


</div>
</div>
<div class="modal-footer" style="position:fixed !important;">
<div class="col s6 push-s5">
<button class="center-align btn waves-effect waves-light light-blue darken-4" type="submit" name="action" id="enter_submit">A votar!</button>
</div>
</div>

<?php echo form_close();?>


CONTROLLER

public function verification()
{
$this->load->model("votes");
if ($this->input->is_ajax_request()) {

if ($this->form_validation->run('enter_form')) {
//some code
}
//else should display validation_errors() on form view

$params["email"] = $this->input->post('enter_email');
$params["password"] = $this->input->post('enter_password');

$result = $this->votes->verification($params);

if ($result==null) {
$params["status"]= 0;
$success= $this->votes->user_record($params);
return $success;
}
else {
$params["status"]= 1;
$success= $this->votes->user_record($params);
return $success;
}

}
}


JAVASCRIPT

$("#enter_form").submit(function(event) {

event.preventDefault();

var data = $(this).serialize();
alert(data);

$.post(baseurl+"votos/verification",
data,
function(data) {

if (data == 1) {
alert("registrado");
}
else {
alert("No estabas registrado, ahora si");
}
}

);

});


FORM_VALIDATION.PHP (input rules)

'enter_form'
=> array(

array('field' => 'enter_email','label' => 'Email','rules' => 'required|is_string|valid_email'),
array('field' => 'enter_password','label' => 'Password','rules' => 'required|is_string'),

),

Answer

It's because you send form via ajax request. This code doesn't run after you submit form.

 $errors= validation_errors('<li>','</li>');
                if($errors != ""){
                      ?>
                          <div class="red lighten-2">
                            <ul>
                              <?php echo $errors;?>
                            </ul>
                          </div>
                      <?php
                }

You should catch errors on server and send them back like this:

    if ($this->form_validation->run('enter_form') === true) {
        //some code
    }else{
        $errors = validation_errors();
        echo json_encode(array("success"=>false, "message"=>$errors));
        die();
    }

Then in ajax callback you put message in a div and display them.

Comments