2

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');
}
4
  • What's wrong with it? Commented Apr 8, 2014 at 12:02
  • Please add some information about your error message / error logs to let us help you. Commented Apr 8, 2014 at 12:02
  • 1
    You need use $this->form_validation->run() to call to test. Commented Apr 8, 2014 at 12:02
  • hi 110precent , i added this $this->form_validation->run() . it is working fine .But i have set validation rules for fields na ...it is not working .Directly it is redirect to form action ... Commented Apr 8, 2014 at 12:05

3 Answers 3

2

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
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

i want to check only this function $this->form_validation->set_rules('username', 'Username', 'required'); else everything is working ...
If you do not call "run" method, does will not work as expected. After call "$this->form_validation->run();" should be work. try.
i tried ... before executing $this->form_validation->run() i have written code for validation right ? so first validation should work ..right ?
thanks wrivas !! actually i did one mistake !! i was not redirecting to that page in which i written validation_error() .you are right .
2
<?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

2 Comments

thnaks for giving help ...these thing i tried ...same code only i implemented .
no error is showing .it is redirecting to page which i written in my controller .See my controller here ...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 $this->load->view('formsuccess'); } else { $this->load->view('formerror'); } }
0

Try this

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

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.