Welcome Guest, Not a member yet? Register   Sign In
Controller callback vs form validation organization?
#1

[eluser]gwerner[/eluser]
I have a form that allows users to select a start date and time, as well as, an end date and time. These are able to be selected as 4 form inputs.

My question is I have a form validation rule that checks to make sure a user has selected a start date and time that are less than the end date and time. The only way I'm seeing to validate this is to add a callback within the controller. I'd rather it be in MY_form_validation class. It seems like this would be the better organizational place for this type of code? My controller feels full and it seems this small blob of code could get lost.

However, to get the callback to work I need the input->posts to compare against. If I move the code to the the validation class I won't have access to the inputs unless I set them as independent variables and pass them as parameters. I'm not sure I'll be able to do this and still access the form validation errors though? Is there a better way to achieve this, or is what I'm doing correct?

The form validation
Code:
$this->form_validation->set_rules('messageStartDate', 'Start Date', 'trim|required|xss_clean|valid_date|callback_greater_date');

The callback
Code:
function greater_date()
{
  $start_date = date('Y-m-d H:i:s', strtotime($this->input->post('messageStartDate'))) . ' ' . $this->input->post('messageStartTime');
  $end_date = date('Y-m-d H:i:s', strtotime($this->input->post('messageEndDate'))) . ' ' . $this->input->post('messageEndTime');

  if ($start_date <= $end_date)
  {
   return TRUE;
  }
  return FALSE;
}
#2

[eluser]CroNiX[/eluser]
If you extend the Form_validation class, it's not a callback any longer. It's only a callback function if it exists in the controller. It's now a regular validation function like any of the others that come with form_validation. So you shouldn't have "callback_" as the prefix to the rule name in the set_rules().
#3

[eluser]Aken[/eluser]
What you're doing is fine. I'd only turn it into a validation rule of some sort if you use the same callback code in multiple controllers (don't repeat yourself).

Also a random tidbit - you can return the expression check instead of doing an entire if/else check:

Code:
return ($start_date <= $end_date);
#4

[eluser]CroNiX[/eluser]
And you actually can access CI's functions from within libraries, or extended libraries using

Code:
$CI =& get_instance();
$some_post_var = $CI->input->post('some_post_var');
#5

[eluser]gwerner[/eluser]
[quote author="CroNiX" date="1344898301"]If you extend the Form_validation class, it's not a callback any longer. It's only a callback function if it exists in the controller. It's now a regular validation function like any of the others that come with form_validation. So you shouldn't have "callback_" as the prefix to the rule name in the set_rules().[/quote]

I think I may have been unclear in my first post. I am currently including this in the controller. I am then accessing it with the callback_ in the form validation in the controller. I just wanted to know which way is better. As a callback or extending the Form_validation. It looks like either is ok depending on if it will be used beyond this single controller.

[quote author="Aken" date="1344901731"]What you're doing is fine. I'd only turn it into a validation rule of some sort if you use the same callback code in multiple controllers (don't repeat yourself).

Also a random tidbit - you can return the expression check instead of doing an entire if/else check:

Code:
return ($start_date <= $end_date);
[/quote]

Makes sense and thanks for the reminder on the expression check. Old habits.

[quote author="CroNiX" date="1344904954"]And you actually can access CI's functions from within libraries, or extended libraries using

Code:
$CI =& get_instance();
$some_post_var = $CI->input->post('some_post_var');
[/quote]

I tend to forget this as well, thanks for the reminder.




Theme © iAndrew 2016 - Forum software by © MyBB