CodeIgniter Forums
private callback functions? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: private callback functions? (/showthread.php?tid=46539)



private callback functions? - El Forum - 11-04-2011

[eluser]dadamssg[/eluser]
How do you set private callback functions? It seems they must be public to use codeigniter's callback form validation functions. In the example in the user guide(below) a random user could go to "http://www.codeigniter.com/index.php/form/username_check". In this instance the user probably wouldn't see anything but if the function was a little more complicated and certain inputs were expected they could get a bunch of errors.


Code:
<?php

class Form extends CI_Controller {

function index()
{
  $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');

  $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required');

  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('myform');
  }
  else
  {
   $this->load->view('formsuccess');
  }
}

function username_check($str)
{
  if ($str == 'test')
  {
   $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

}
?>