CodeIgniter Forums
validation callback rules and dynamic text in the error mesage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: validation callback rules and dynamic text in the error mesage (/thread-18511.html)



validation callback rules and dynamic text in the error mesage - El Forum - 05-08-2009

[eluser]deanf7[/eluser]
I'm using call back function to validate that if a user checks a specific textbox they also enter text in the accompanying input field. There are a number of such fields in my form, and I didn't want to write a callback for each one. It correctly generates the error for each field, but it won't print the custom text

Here's the code for the call back:
Code:
function checkActivities($activity, $time_spent) {
   if(isset($activity) && $time_spent != '') {
    return true;
   }
   $this->form_validation->set_message('checkActivities', "If you checked an activity (" . $activity . ") as completed, please list the number of minutes spent on the activity.");
    
    return false;
}

and an example of the callback rule is:
Code:
$this->form_validation->set_rules('rev_agenda_time', 'CheckActivities', 'callback_checkActivities[rev_agenda, rev_agenda_time]');

Any ideas why $activity won't print out in the message?

Thanks


validation callback rules and dynamic text in the error mesage - El Forum - 05-08-2009

[eluser]deanf7[/eluser]
As a follow up to my first message. This is what gets printed to the screen:
If you checked an activity () as completed, please list the number of minutes spent on the activity.


validation callback rules and dynamic text in the error mesage - El Forum - 05-10-2009

[eluser]Cro_Crx[/eluser]
Hey dean

The callback functions only take one parameter which is the name of the input variable your validating. So in your example $activity should work fine but $time_spent will be assigned to null. To get around this you can access the time_spent variable by using the input value
Code:
$this->input->post('time_spent')
or similar.

I'm assuming that in the example the callback is being called by the activity input. If it's being called by time spent then you need to specify the activity with
Code:
$this->input->post('activity')
instead!


validation callback rules and dynamic text in the error mesage - El Forum - 05-11-2009

[eluser]deanf7[/eluser]
Thanks for the reply. I have a number of such fields in the same form and I was hoping to to simply pass the field name in as a variable without having to to explicitly hardcode it like $this->input->post('activity'), but like $this->input->post($field_name).

Thanks


validation callback rules and dynamic text in the error mesage - El Forum - 05-11-2009

[eluser]deanf7[/eluser]
if it helps anyone else I figured this one out.

You'd write your callback message to read like this:
Code:
$this->form_validation->set_message('checkActivities', "If you checked (%s) as completed, please list the number of minutes spent on the activity.");

Then in your form validation you'd write something like:
Code:
$this->form_validation->set_rules('rev_agenda_time', 'Review Agenda', 'callback_checkActivities[rev_agenda, rev_agenda_time]');

Which prints : If you checked (Review Agenda) as completed, please list the number of minutes spent on the activity.