CodeIgniter Forums
Callback Validation and Multiple Parameters - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Callback Validation and Multiple Parameters (/showthread.php?tid=33545)



Callback Validation and Multiple Parameters - El Forum - 08-30-2010

[eluser]Namsu[/eluser]
For example I have:

$this->form_validation->set_rules('datepicker', 'Date', 'callback__dateRange[' . $dates . '||' . $datef . ' ]|required');

That is probably wrong. How do I add multiple parameters to that callback__function() ?


Callback Validation and Multiple Parameters - El Forum - 08-30-2010

[eluser]cahva[/eluser]
You can only give one parameter to callback. But that doesnt seem to be right what you are doing. Where does $dates and $datef come and what do they contain?

If its some dynamic date depenging on current date, you can do that functionality straight in callback, no need to pass any variables(other than the value itself ofcourse). Or you can use 2 callbacks, something like:
Code:
$this->form_validation->set_rules('datepicker', 'Date', 'required|callback_mindate[2010-09-01]|callback_maxdate[2010-10-5]');



Callback Validation and Multiple Parameters - El Forum - 08-30-2010

[eluser]WanWizard[/eluser]
It is possible to pass multiple parameters to a function. I have several, for example 'unique':
Code:
$this->form_validation->set_rules('userid', 'User', 'required|unique[users.id]');

Code:
function unique($value, $params)
{
    list($table, $field) = explode(".", $params, 2);

    if ( ! empty($table) && ! empty( $field ) )
    {
        $CI =& get_instance();
        $CI->db->select($field);
        $CI->db->from($table);
        $CI->db->where($field, $value);
        $CI->db->limit(1);
        $query = $CI->db->get();

        if ($query->row())
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    else
    {
        show_error('Call to Form_validation::unique() failed, parameter not in "table.column" notation');
    }
}



Callback Validation and Multiple Parameters - El Forum - 08-30-2010

[eluser]cahva[/eluser]
Nope.. just one parameter, you just explode the string inside it within the function Wink But yeah, that works too.


Callback Validation and Multiple Parameters - El Forum - 08-31-2010

[eluser]Namsu[/eluser]
The implode and explode isn't working for me! I end up with only having one element in the array at position 0. Also used print_r to tell me. Any ideas?

$dates = $this->input->post('datepickers');
$datef = $this->input->post('datepickere');
$darr = array (0 => $dates, 1 => $datef);
$imp = implode(",", $darr);

$this->form_validation->set_rules('datepickers', 'Date Range', 'callback__dateRange[' . $imp . ']');

function _dateRange($date) {

$dated = explode(",", $date);
$date1 = strtotime($dated[0]);
$date2 = strtotime($dated[1]);
print_r($dated);

if ($date1 >= $date2) {

$this->form_validation->set_message('_dateRange', 'Invalid Date Range.' . $dated[0] . $dated[1]);
return FALSE;
} else {

return TRUE;
}
}


Callback Validation and Multiple Parameters - El Forum - 08-31-2010

[eluser]cahva[/eluser]
callback will send 2 parameters, value and parameters. Now you're only sending the value of datepickers.

Try to change the function to:
Code:
function _dateRange($val,$date) {

BTW, please use code tags if you paste code. Easier to read and all that..


Callback Validation and Multiple Parameters - El Forum - 09-02-2010

[eluser]Namsu[/eluser]
[quote author="cahva" date="1283303000"]callback will send 2 parameters, value and parameters. Now you're only sending the value of datepickers.

Try to change the function to:
Code:
function _dateRange($val,$date) {

BTW, please use code tags if you paste code. Easier to read and all that..[/quote]

Thank you for that, function is working properly and one less variable needed as the default is the form field.


Callback Validation and Multiple Parameters - El Forum - 09-07-2010

[eluser]Jason Tan Boon Teck[/eluser]
[quote author="WanWizard" date="1283221599"]It is possible to pass multiple parameters to a function. I have several, for example 'unique':
Code:
$this->form_validation->set_rules('userid', 'User', 'required|unique[users.id]');

Code:
function unique($value, $params)
{
    list($table, $field) = explode(".", $params, 2);

    if ( ! empty($table) && ! empty( $field ) )
    {
        $CI =& get_instance();
        $CI->db->select($field);
        $CI->db->from($table);
        $CI->db->where($field, $value);
        $CI->db->limit(1);
        $query = $CI->db->get();

        if ($query->row())
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    else
    {
        show_error('Call to Form_validation::unique() failed, parameter not in "table.column" notation');
    }
}
[/quote]

Hi,

How do you make the show_error to display the error warning in the form?

Thx,

Jason


Callback Validation and Multiple Parameters - El Forum - 09-08-2010

[eluser]cahva[/eluser]
[quote author="Jason Tan" date="1283934453"]
How do you make the show_error to display the error warning in the form?
[/quote]

The almighty RT(F)M Wink