user3510721
user3510721

Reputation: 55

Codeigniter validation is not working

I am new in codeigniter .I have done validation for my project .But it is not working fine .I have written my all code here .First is view page and second is my controller page.Please help anyone

<?php $this->load->helper('form');  
  echo validation_errors();  
  echo form_open('SM_in_controller/sm_login_action');
?>

  <input class="login_input" type="text" placeholder="Username" name="username" id="username"/>
  <input type="submit" value="Login" class="login_button" id="login_button"/>
</form>  

and my controller

public function sm_login_action() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username', 'Username', 'required');
}

Upvotes: 2

Views: 93

Answers (3)

Nehil Mistry
Nehil Mistry

Reputation: 1109

<?php

class Form extends CI_Controller {

    public function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }

    public function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

}
?>

try this for more details. https://www.codeigniter.com/user_guide/libraries/form_validation.html

Upvotes: 2

Rebecca
Rebecca

Reputation: 413

Try this

<input class="login_input" type="text" placeholder="Username" name="username" id="username" required/>

Hope this helps

Upvotes: 0

wrivas
wrivas

Reputation: 509

In your controller, when you call the "run" method is when the proccess is done:

public function sm_login_action() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username', 'Username', 'required');

   if( $this->form_validation->run() ) { // Return TRUE on success
        // Success
   } else {
      // Failure
  }
}

Upvotes: 2

Related Questions