Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Codeigniter form validation

I am having some problem in my login form like this

enter image description here

I want to display an individually error beside of each field

here is the controller

function index()
{      
        $this->form_validation->set_rules('username','Username','trim|required|exact_length[4]|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[40]|xss_clean|callback_login');
        $this->form_validation->set_rules('jabatan','Jabatan','trim|required|xss_clean');

        if($this->form_validation->run() == false)
        {
            $this->load->view('login');
        }
        else
        {
            $this->load->view('welcome_message');
        }

}

function login()
{
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $jabatan = $this->input->post('jabatan');

    $value = $this->m_login->login($username,$password,$jabatan);

    if($value)
    {
        return true;
    }
    else
    {
        $this->form_validation->set_message('login', 'password salah');
        //delete redirect() and showing blank white screen
        return false;

    }

I made the <?php echo form_error(); ?> beside each field

<?php echo form_open('c_login/login'); ?>
            <table>
                <tr>
                    <td>Username</td>
                    <td><?php $inusername=array('name' => 'username', 'class' => 'GUI'); echo form_input($inusername); ?></td>
                    <td class="error"><?php echo form_error('username'); ?></td>
                </tr>

                <tr>
                    <td>Password</td>
                    <td><?php $inpassword=array('name' => 'password', 'class' => 'GUI', 'type' =>'password'); echo form_input($inpassword); ?></td>
                    <td class="error"><?php echo form_error('password'); echo $this->session->flashdata('login'); ?></td>
                </tr>

                <tr>
                    <td>Jabatan</td>
                    <td><?php $injabatan=array('keuangan' => 'keuangan', 'admin' => 'admin', 'hd' => 'head divisi', 'direktur' => 'direktur'); echo form_dropdown('jabatan',$injabatan,'keuangan','class = "gui"'); ?></td>
                    <td class="error"><?php echo form_error('jabatan'); ?></td>
                </tr>

                 <tr>
                    <td></td>
                    <td><?php $insubmit=array('name' =>'login','class' =>'button','value' => 'Login'); echo form_submit($insubmit); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
                    <td class="error"><?php echo form_error(); ?></td>
                </tr>
            </table>
            <?php echo form_close(); ?>

How come the form_error() function is not echoing anything when I click login with the username and password field left blank?

like image 513
Cignitor Avatar asked Dec 21 '22 15:12

Cignitor


2 Answers

you know problem in your validation you're checking validation in index method of your class not in login method of your class and in your form you have given action to ci_login/login method which is redirecting if login is failed and its clearing form validation validate your fields on login method also and put all error message in session and display, i have made some changes in script have look here on this link Code modified

for this changes you have to use url helper

function index()
{
    $this->form_validation->set_rules('username','Username','trim|required|exact_length[4]|xss_clean');
    $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[40]|xss_clean|callback_login');
    $this->form_validation->set_rules('jabatan','Jabatan','trim|required|xss_clean');

    if($this->form_validation->run() == false)
    {
        $this->load->view('login');
    }
    else
    {
        //to check if the validation run correctly
        //$this->load->view('welcome_message');

        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $jabatan = $this->input->post('jabatan');

        $value = $this->m_login->login($username,$password,$jabatan);

        if($value)
        {
            redirect('welcome_message');
            //return true;
        }
        else
        {
            $this->form_validation->set_message('login', 'password salah');
            redirect('c_login',$login); //i want to pass $login into login form, then print
            return false;               //them as a form_error

        }
    }

}


<?php echo form_open(uri_string()); ?>
<table>
    <tr>
        <td>Username</td>
        <td><?php $inusername=array('name' => 'username', 'class' => 'GUI'); echo form_input($inusername); ?></td>
        <td class="error"><?php echo form_error('username'); ?></td>
    </tr>

    <tr>
        <td>Password</td>
        <td><?php $inpassword=array('name' => 'password', 'class' => 'GUI', 'type' =>'password'); echo form_input($inpassword); ?></td>
        <td class="error"><?php echo form_error('password'); echo $this->session->flashdata('login'); ?></td>
    </tr>

    <tr>
        <td>Jabatan</td>
        <td><?php $injabatan=array('keuangan' => 'keuangan', 'admin' => 'admin', 'hd' => 'head divisi', 'direktur' => 'direktur'); echo form_dropdown('jabatan',$injabatan,'keuangan','class = "gui"'); ?></td>
        <td class="error"><?php echo form_error('jabatan'); ?></td>
    </tr>

     <tr>
        <td></td>
        <td><?php $insubmit=array('name' =>'login','class' =>'button','value' => 'Login'); echo form_submit($insubmit); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
        <td class="error"><?php echo form_error(); ?></td>
    </tr>
</table>
<?php echo form_close(); ?>
like image 87
umefarooq Avatar answered Jan 07 '23 12:01

umefarooq


index will not execute when login does.

If appears that maybe you think the set_rules() functions will execute when you submit the form. They won't. The form will submit to 'c_login/login', which is the login() function inside your c_login controller.

You need to move your form validation logic into login(), and have index() just echo the view for the first time.

Your Controller

function index(){      
    $this->load->view('login');
}

function login(){
    $this->form_validation->set_rules('username','Username','trim|required|exact_length[4]|xss_clean');
    $this->form_validation->set_rules('password','Password','required|xss_clean|correct');
    $this->form_validation->set_rules('jabatan','Jabatan','trim|required|xss_clean');

    if($this->form_validation->run() == false){
        //your validation messages will be taken care of.
        $this->load->view('login');
    }
    else{
        $p = $this->input->post();
        if($this->m_login->login($p['username'],$p['password'],$p['jabatan'])){
            //redirect() user to logged in area.
        }
        else{
            $this->form_validation->set_message('login', 'password salah');
            $this->load->view('login');
        }
    }
}

Also, you shouldn't be trimming passwords. The space character is a perfectly valid suffix or prefix in a password. I've removed trim for you. I've also removed the min_length and max_length. This is something you'd want to enforce on a signup page, are you sure it really adds any value on a login page?

like image 42
Jordan Arseno Avatar answered Jan 07 '23 13:01

Jordan Arseno