0
votes

I am trying to validate email and password using the CodeIgniter's form_validation library. But when I typed wrong email or password the error message of validation_rules is not displayed.

The controller:

public function anuncios() 
{
    $usr=$this->input->post('Usuario');
    $this->input->post('Contrasenya');

    $this->form_validation->set_rules('drcorreo','Nombre de usuario',
            'trim|required|min_length[5]|xss_clean');
     $this->form_validation->set_rules('contrasena','Contraseña',
     'trim|required|min_length[8]|md5|xss_clean');

    if($this->form_validation->run()) 
     {
        $this->load->model('modelo_usuarios');

        if($this->modelo_usuarios->puede_entrar()) 
         {
            echo "Credenciales correctos";
            $this->load->model("modelo_bd");
            $data['vanc']=$this->modelo_bd->datos();
            $this->load->view('vancios',$data);

            return true;
         }
        else { //this one is never displayed

                                    echo "Credenciales incorrectos";
                echo "Usuario o contrasenya incorrectos<br /><br />";
                $this->load->view('indice');

                return false;
             }
     }  
    else { //this one is displayed but not the rules specified in 
                   //form_validation_lang.php file

            echo "Las reglas no son validas";
            $this->load->view('indice');
         }

}

View:

<div id="acformulario"> 
    <form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
        <label for="correo" id="dcorreo">Direcci&oacute;n de correo</label>
        <input type="text" name="drcorreo" id="dcc"/><br /><br />
        <label for="contrasenya" id="cont">Contrase&ntilde;a</label>
        <input type="password" name="contrasena" id="cmcont"/><br /><br />
        <!--<label for="enviar"></label>-->
        <input type="submit" name="envia" id="bentrar" value="Entrar" />    
    </form>
</div>  

Why error messages are not displayed?

Thanks.

2
also why not change your config (about line 323) to xss_clean instead of adding it to every rule? Change it to $config['global_xss_filtering'] = TRUE; - Rooneyl

2 Answers

0
votes

i assume you call "form" helper in your autoload.php file,

use your view like this;

<div class="errors"><?php echo validation_errors(); ?></div>
<div id="acformulario"> 
    <form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
        <label for="correo" id="dcorreo">Direcci&oacute;n de correo</label>
        <input type="text" name="drcorreo" id="dcc"/><br /><br />
        <label for="contrasenya" id="cont">Contrase&ntilde;a</label>
        <input type="password" name="contrasena" id="cmcont"/><br /><br />
        <!--<label for="enviar"></label>-->
        <input type="submit" name="envia" id="bentrar" value="Entrar" />    
    </form>
</div>

the point is "validation_errors()" function in here.

0
votes

The 'can not enter' message is passed back after the rules have been passed. It is inside of the if statement when the form validation is true.

If you want this message to be displayed either load the message into the view, or set it as flash data.

Why not make the puede_entrar() model call into a custom callback form validation rule, then it can be added to the rules that are set.

Look here for information on custom callbacks.

This is how I would code your controller;

<?php

class Foo extends CI_Controller {
    public function anuncios() {
    // xxs_clean set globally in config
        $this->form_validation->set_rules('drcorreo','Nombre de usuario', 'trim|required|min_length[5]|callback_puede_entrar');
        // should use sha1 at least for hashing, see http://www.freerainbowtables.com/tables/
        $this->form_validation->set_rules('contrasena','Contrase&ntilde;a', 'trim|required|min_length[8]|md5');

        if($this->form_validation->run()) {
            $this->load->model('modelo_bd');
            $data['vanc']=$this->modelo_bd->datos();
            $this->load->view('vancios',$data);
        } else { 
            // redisplay form with validation errors
            $this->load->view('indice');
        }
    }

    public function puede_entrar($val) {
        $this->load->model('modelo_usuarios');

        if($this->modelo_usuarios->puede_entrar()) {
            return TRUE;
        } else {
            $this->form_validation->set_message('puede_entrar', 'Las reglas no son validas.');
            return FALSE;
        }
    }
}

The view (assuming you have autoloaded or have loaded $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); elsewhere);

<div id="acformulario"> 
    <form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
        <?php echo form_error('drcorreo'); ?>
        <label for="correo" id="dcorreo">Direcci&oacute;n de correo</label>
        <input type="text" name="drcorreo" id="dcc"/><br /><br />
        <label for="contrasenya" id="cont">Contrase&ntilde;a</label>
        <?php echo form_error('contrasena'); ?>
        <input type="password" name="contrasena" id="cmcont"/><br /><br />
        <!--<label for="enviar"></label>-->
        <input type="submit" name="envia" id="bentrar" value="Entrar" />    
    </form>
</div>