4

I have two numeric fields to collect data from users. Need to validate it using codeigniter form validation class.

Conditions:

  1. First field can be zero
  2. Second field cannot be zero
  3. First field should not be equal to second field
  4. Second field should be greater than first field

Currently I use

$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');

$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero');

But, how to validate for 3rd and 4th condition mentioned above?

Thanks in advance.

3 Answers 3

18

Thanks dm03514. I got it working by the below callback function.

$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']');

and the callback function is:

function check_equal_less($second_field,$first_field)
  {
    if ($second_field <= $first_field)
      {
        $this->form_validation->set_message('check_equal_less', 'The First &amp;/or Second fields have errors.');
        return false;       
      }
      return true;
  }

Everything seems to be working fine now :)

Sign up to request clarification or add additional context in comments.

Comments

4

You can write your own validation function for 3, and 4 using callbacks

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

Example from doc

<?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;
        }
    }

}
?>

1 Comment

But, how to pass the other field values in the callback function?? In the CI Documentation, $str is set to 'test' in the callback function; but I need to pass the first field value to the call back function of second field.
0

If you are using HMVC and the accepted solution doesn't work then add the following line after initialization in your controller

$this->form_validation->CI =& $this;

So it will be

$this->load->library('form_validation');
$this->form_validation->CI =& $this;  

in your controller.

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.