0

I want to use a function to validate dates with a parameter...but I have all validations in a config array.

config = array(
'page1/send'   => array(
    array(
            'field' => 'name',
            'label' => 'lang:name',
            'rules' => 'required'
         )
),    

'page2/send'   => array(
                            array(
                                    'field' => 'name',
                                    'label' => 'lang:name',
                                    'rules' => 'required'
                            ),
                             array(
                                    'field' => 'date1',
                                    'label' => 'lang:date',
                                    'rules' => 'required|'
                            )
                            array(
                                    'field' => 'date2',
                                    'label' => 'lang:date',
                                    'rules' => 'required|callback_date_compare'
                            )

                        ),

I would like to pass an additional parameter to "callback_date_compare" in this case the other field (date1).

Without setting the rules in an array I could do it in this way if "$date1" is the value of the post['date1'] and it worked perfectly :

$this->form_validation->set_rules('date2', 'lang:date', 'required|callback_date_compare[' . $date1 . ']');

I need to do it inside the array because I have all validations inside it and I tried to do it the same way inside the $config array but it didn´t work, something like:

                                  array(
                                        'field' => 'date2',
                                        'label' => 'lang:date',
                                        'rules' => 'required|callback_date_compare[date1]'
                                )

Any help is appreciated.

3 Answers 3

3

In your config array

array(
        'field' => 'date2',
        'label' => 'lang:date',
        'rules' => 'required|callback_date_compare[date1]'
   )

in your date compare callback

function date_compare($value, $field_name)
{
    // Get the value in the field
    $field = $_POST[$field_name];

    if ($value != $this->input->post($field))
    {
          $this->form_validation->set_message('date_compare', 'Dates are different');
          return FALSE;
    }
    else
    {
         return TRUE;
    }

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

Comments

1

Since your config is global, it might be a good idea to have the function global aswell.

Create MY_Form_validation.php in libraries/ :

<?php

class MY_Form_validation extends CI_Form_validation {

    function date_compare($str_start, $str_key) {

        $bool   =   ($str_start == $this->input->post($str_key));
        if ( ! $bool)
            $this->form_validation->set_message('date_compare', 'Dates are different');

        return $bool;

    }

}

Then set the rule date_compare[date1].

4 Comments

Actually I have the function date_compare in My_controller to use it from any controller. Thanks anyway Robin.
Hmm I wonder what happends if you use site.com/date_compare then?
With site.com/date_compare Returns 404 Pagen Not Found" and with site.com/date_compare(param1, param2) returns "An Error Was Encountered The URI you submitted has disallowed characters."
site.com/any_controler/date_compare will work - but it doenst matter- just append an "underscore" to "date_compare" to make it private - and then change your callback to "callback__date_compare" (note the double underscore)
0

create a function called date_compare:

public function date_compare($date2)
{

if ($date2 != $this->input->post("date1"))
{
$this->form_validation->set_message('date_compare', 'Dates are different');
return FALSE;
}
else
{
return TRUE;
}

}

config:

'page2/send'   => array(
                            array(
                                    'field' => 'name',
                                    'label' => 'lang:name',
                                    'rules' => 'required'
                            ),
                             array(
                                    'field' => 'date1',
                                    'label' => 'lang:date',
                                    'rules' => 'required|'
                            )
                            array(
                                    'field' => 'date2',
                                    'label' => 'lang:date',
                                    'rules' => 'required|callback_date_compare'
                            )

                        ),

1 Comment

Thanks Adam, the problem is I would like to use the function in other´s forms and I want it to be "generic". If I do it as you say I can´t change the name of the input.. date1, date2, etc.. The function I have is to compare two dates ( start and end) and control that the "start" date not greater than "end", but I use it in other places

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.