Welcome Guest, Not a member yet? Register   Sign In
form_validation->run() not working as expected
#1

[eluser]Unknown[/eluser]
I am learning to use CI (need to use it to re-write an existing app) and have run into an issue where
Code:
form_validation->run()
doesn't work as expected. Here is the code:

Code:
<?php
class Validator extends Controller {

  function Validator()
  {

    // load controller parent
    parent::Controller();

    // load 'url' helper
    $this->load->helper('url');

    // load 'form' helper
    $this->load->helper('form');

    // load 'validation' class
    $this->load->library('form_validation');

  }

  function index()
  {

    // set validation rules
    $rules['first_name']="required|min_length[2]|max_length[20]";
    $rules['last_name']="required|min_length[2]|max_length[20]";
    $this->form_validation->set_rules($rules);

    // check if form has been submitted properly
    if ( $this->form_validation->run() == FALSE )
    {
      // redisplay web form
      $this->load->view('form_view');

    }

    // display success web page
    else
    {
      $this->load->view('success_view');
    }

  }

}
?>

The web form is displayed properly. If I enter incorrect values (or fail to enter a value) for one of the fields being validated, I expect to get an error message. Instead, the test:

Code:
if ( $form_return = $this->form_validation->run() == FALSE )
{
}

is FALSE (form_validation->run() returns TRUE). Every time. When I use validation->run() the test succeeds (validation->run() returns FALSE).

I am using CodeIgniter 1.7.2 and would like to use the Form_validation library as it lets me set custom error messages.

Is there some trick that I'm missing?

Thanks!
#2

[eluser]JHackamack[/eluser]
I believe your second test:

if ( $form_return = $this->form_validation->run() == FALSE )
{
}

is incorrectly written as its both setting values and comparing values,

I would instead try

$form_return = $this->form_validation->run();

if($form_return == FALSE )
{
}

Does that work correctly?
#3

[eluser]Dyllon[/eluser]
This isn't a bug, you are setting your rules incorrectly so when you run the validation it doesn't have any rules to validate against so it returns TRUE.

Read the userguide section for form_validation, the method for setting rules that you are using got deprecated with the old validation library.
#4

[eluser]Unknown[/eluser]
It was the set_rules() invocation that caused the problem. That may have come from following an older tutorial or user guide. Everything's working properly now. The offending code now looks like this:

Code:
// set validation rules
    $rules = array(
        array('field' => 'first_name',
              'label' => 'First Name',
              'rules' => 'required|min_length[2]|max_length[20]') ,
        array('field' => 'last_name',
              'label' => 'Last Name',
              'rules' => 'required|min_length[2]|max_length[20]')
      ) ;

    $this->form_validation->set_rules($rules);

Thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB