CodeIgniter Forums

Full Version: Private function and Custom form validation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[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?

El Forum

[eluser]CroNiX[/eluser]
Yes, http://ellislab.com/codeigniter/user-gui...ml#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

El Forum

[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.

El Forum

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

El Forum

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

El Forum

[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."

El Forum

[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.

El Forum

[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.

El Forum

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

El Forum

[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;
     }
}
Pages: 1 2