CodeIgniter Forums
Private function and Custom form validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Private function and Custom form validation (/thread-53030.html)

Pages: 1 2


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]BenAbrams[/eluser]
Hi Guys

I have a custom field validation rule in a config file set up, something similar to to the below:

Code:
array(
     'field' => 'check_terms',
     'label' => 'Terms of service',
     'rules' => 'callback_check_terms'
)

A very simple function in my controller below just checks the input and provides a custom error message

Code:
public function check_terms($terms=0){
     if ($terms != 'agree')
     {
          $this->form_validation->set_message('check_terms', 'The terms of service must be read and agreed to before continuing.');
          return FALSE;
     }
     else
     {
          return TRUE;
     }
}

I wanted to make the function above 'private' so it couldn't be accessed by the URI but obviously this prevents the helper from accessing the function. I cant add the underscore to the function name because the callback doesn't seem to accept it. I was thinking of using the _remap function to send 'check_terms' in the uri to the 404 but this seems a little overkill.

Can anyone suggest a more elegant solution?


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]CroNiX[/eluser]
Yes, http://ellislab.com/codeigniter/user-guide/general/controllers.html#private

Code:
function _check_terms(){}

$this->form_validation->set_rules(fieldname, labelname, 'callback__check_terms');//2 underscores

Since callback prefixes are "callback_", and the private function is "_check_terms", it ends up being "callback__check_terms" with 2 undrescores


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]InsiteFX[/eluser]
Ya @CroNiX, he said he tried that but then the helper will not see it.

lol the old double underscore strikes again.



Private function and Custom form validation - El Forum - 07-08-2012

[eluser]CroNiX[/eluser]
I don't think he called the callback correctly, but he didn't post enough code to really know...


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]InsiteFX[/eluser]
I think your right CroNiX, looks like the old double underscore problem again.



Private function and Custom form validation - El Forum - 07-08-2012

[eluser]BenAbrams[/eluser]
If I double underscore, instead of showing my custom message, it shows "Unable to access an error message corresponding to your field name."


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]CroNiX[/eluser]
Did you change the name of your error message to "_check_terms" since you changed the function name? They have to be the same.


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]BenAbrams[/eluser]
[quote author="CroNiX" date="1341766629"]Did you change name of your error message to "_check_terms" since you changed the function name? They have to be the same.[/quote]
I did indeed.


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]CroNiX[/eluser]
Strange, works just fine from here.


Private function and Custom form validation - El Forum - 07-08-2012

[eluser]InsiteFX[/eluser]
Code:
array(
     'field' => 'check_terms',
     'label' => 'Terms of service',
     'rules' => 'callback__check_terms' // double underscore
)

Code:
public function _check_terms($terms=0){
     if ($terms != 'agree')
     {
          $this->form_validation->set_message('_check_terms', 'The terms of service must be read and agreed to before continuing.'); // according to CroNiX
          return FALSE;
     }
     else
     {
          return TRUE;
     }
}