• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Private function and Custom form validation

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

#2
[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

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

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

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

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

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

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

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

#10
[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;
     }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.