Welcome Guest, Not a member yet? Register   Sign In
Form validation callback array
#1

[eluser]veledrom[/eluser]
Hi,

I want to third parameter to callback function but am failing to do so.

Please help.

Thanks

This works fine:
Code:
$this->form_validation->set_rules('travel_date_from', 'From', 'trim|required|exact_length[10]|callback_compare_travel_dates[' . $this->input->post('travel_date_until') . ']');

public function compare_travel_dates($from_travel_date, $until_travel_date)
{
   //Do whatever you want
}

I want to do this now but cannot set $this->form_validation->set_rules part:
Code:
public function compare_travel_dates($from_travel_date, $until_travel_date, $earliest_date)
{
   //Do whatever you want
}
#2

[eluser]vbsaltydog[/eluser]
pass an array to the callback function
#3

[eluser]CroNiX[/eluser]
The callback function will only receive 2 parameters. The first is the value of the form field that was submitted and the second is a STRING of whatever is in the brackets of a callback (if you use brackets).
Code:
function compare_travel_dates($submitted_date, $everything_in_the_brackets){}

So I'm not sure how you plan on using 3 different dates in there (from_travel_date, $until_travel_date, $earliest_date)...
Unless they are different form fields, in which case they wouldn't get passed to your val function. Within the function you would:
Code:
function compare_travel_dates($submitted_date)
{
  $until_travel_date = $this->input->post('travel_until_date'); //or whatever its called
  $earliest_date = $this->input->post('earliest_date');
  //do your comparisons against $submitted_date
  //...
  //set the error message
  $this->form_validation->set_message('compare_travel_dates', 'Incorrect dates');

  //return pass or fail rule
  return TRUE/FALSE;
}
#4

[eluser]vbsaltydog[/eluser]
create an array of values to pass to the callback
serialize the array
pass the serialized array to the callback
unserialize the string back to an array in the callback

This is the same thing you do to pass arrays as post data.
#5

[eluser]veledrom[/eluser]
Serializing an array is a very good solution. Thanks.
#6

[eluser]LuckyFella73[/eluser]
In case you callback function handles only values of your POST array
you can access the POST values directly without passing anything.

Code:
public function compare_travel_dates()
{
$until_travel_date = $this->input->post('travel_until_date');
$earliest_date = $this->input->post('earliest_date');
$submitted_date = $this->input->post('submitted_date'); // name of your input field

// comparing code here ..
}
#7

[eluser]veledrom[/eluser]
That's good as well. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB