0

Don't get this. I have to validate basic form. Im using CI form_validation library, form validation rules are set just to "required"...if form validation fails it just need to echo validation error in login form. Im using by default <?php echo validation_errors(); ?> Helper is autoloaded. Form functionality is OK it works how it should, only problem is that framework dont display error messagges if dont fill required fields?

login_form.php - VIEW

<?php echo validation_errors(); ?> 

<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>

login.php - CONTROLLER

<?php
Class Login extends CI_Controller {

  function index () {
    $data ['content'] = "login_form";
    $this->load->view("template", $data);
  }

  function validate() {
    $this->load->library("form_validation");


    $this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
    $this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');

    if( $this->form_validation->run() == FALSE) {
        redirect('login/index');
    }
    else {
        redirect("user/profile");
    }
  }

}
?>
7
  • Can you please paste your controller and view Commented Sep 16, 2016 at 18:46
  • redirect('login/index'); dont use redirect load your view Commented Sep 16, 2016 at 18:59
  • Here is full code. Commented Sep 16, 2016 at 18:59
  • or you can try your code just comment out redirect('login/index'); and place <?php echo validation_errors(); ?> you will see errors if there are any Commented Sep 16, 2016 at 19:00
  • It is not view, it is another controller and method that loads profile view... so it is not bug. Commented Sep 16, 2016 at 19:02

2 Answers 2

1

Why don't you use code in controller like this hope will help you just set form action to login.php login.php - CONTROLLER

function index () {
    $data ['content'] = "login_form";
    $this->load->library("form_validation");
    $this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
    $this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
    if( $this->form_validation->run() == FALSE) {
          $this->load->view("template", $data);
    }
    else {
        redirect("user/profile");
     }  
}

login_form.php - VIEW

<?php echo validation_errors(); ?> 

<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

@goran, note that instead of redirecting, the page view is simply reloaded. When you redirect all the data in the current page is lost - including the form_validation class and all of its data. Look carefully at the form validation documentation examples. You will see it done the same way. The above is the correct answer.
@Farhan is only usually == not === in run part
0
if( $this->form_validation->run() == FALSE) {
    redirect('login/index');
}

Don't redirect, else error will not be available. Instead of that keep

if( $this->form_validation->run() == FALSE) {
    $data ['content'] = "login_form";
    $this->load->view("template", $data);
}    

Check the following link for more details:
Form Validation : CodeIgniter User Guide

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.