In this example, username will use custom callback function. to use callback function, you must use callback_ prefix follow by function name in the validation rule.
If your callback function name is username_check(), then you need to write like this callback_username_check.
BTW, if you read documentation from CodeIgniter… the sample didn’t show you how to past parameter to your function if you has more than one parameter. ( http://codeigniter.com/user_guide/libraries/form_validation.html )
So here is the solution.
$this->form_validation->set_rules('username', 'Username', 'callback_username_check[' . $second_param . ']');
callback function
function username_check($str, $second_param) {
if (($str == 'test') && ($second_param == '123')) {
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
} else {
return TRUE;
}
}
edited CodeIgniter sample.
class Form extends CI_Controller {
function index() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$second_param = '123';
$this->form_validation->set_rules('username', 'Username', 'callback_username_check[' . $second_param . ']');
$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, $second_param) {
if (($str == 'test') && ($second_param == '123')) {
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
} else {
return TRUE;
}
}
}
Elijah1475 3:43 pm on April 23, 2012 Permalink |
Hi there would you mind sharing which blog platform you’re working with? I’m planning to start my own blog soon but I’m having a tough time making a decision between BlogEngine/WordPress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!
ibnuyahya 3:53 pm on April 23, 2012 Permalink |
Hi
I’m using WordPress with P2 theme. Anywhere, all blog platform you mentioned does support custom theme. You can make your own unique theme.