Codeigniter callback failure - php

Can anyone see what I'm missing?
I'm using Codeigniter v1.72.
In the doc:
http://codeigniter.com/user_guide/libraries/form_validation.html
It states:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
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;
}
}
In my class User extends Controller
I have in function register:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');
I have also tried
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');
And
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');
function username_check($str)
{
$this->load->model('User_model', '', TRUE);
$taken = $this->User_model->countUsername($str);
if($taken)
{
$this->form_validation->set_message('username_check', 'That username is already taken');
return FALSE;
}
else
return TRUE;
}
There are no errors at all, none of my approaches work, the code behaves like it's not there.

First of all, I'm assuming the rest of your code is correct. It might help to show the whole User class.
You might want to check if CodeIgniter lets you invoke callback functions AND prepping/validator functions in the same rule. If it doesn't allow that, you could call trim, require, and xss_clean in your callback function.
I will say, though, that if it is allowed, then this is definitely the RIGHT form:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');
This is wrong:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');
And this is wrong too:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');
See, in that line, you shouldn't actually be CALLING the function. Rather, you're passing a string to the set_rules() function that it will parse and figure out what function you want to use as a callback.
As the documentation states, whatever the value of username is, will be passed as the argument to your callback function.
EDIT:
I was going to say try this:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
But I don't think it will work. Instead, this should work:
function username_check($str)
{
$this->load->model('User_model', '', TRUE);
$taken = $this->User_model->countUsername($str);
if($taken)
{
$this->form_validation->set_message('username_check', 'That username is already taken');
return FALSE;
}
else if(!$str) {
// This is functioning as the required rule
return FALSE;
}
else {
$str = trim($str);
$str = $this->input->xss_clean($str);
return $str;
}
}

Related

How to set private class to form validation callback in codeigniter

Suppose this is my controller. (copied from CI documentation)
<?php
class Form extends CI_Controller {
public 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|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public 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;
}
}
}
?>
But username_check($str) function is public. According to CI documentation if i want to make a private method I need to add "_" like this.
private function _utility()
{
// some code
}
But how can I set username_check() as private and callback from form validation set_rules?
Should i use DOUBLE underscore "__" i.e callback__username_check?
You can just declare your private function as you have already done:
function _username_check()
{
// some code
}
And in the validation rule, use:
callback__username_check
As I have seen, this must work just fine!
Remember:
The _ prefix would take care of your function privacy, so you do not really have to use the keyword private to declare the function to let the form_validation class access to that function!

Codeigniter form_validation callback is never called

I am setting the form rules in my users model and im having a hard time to reach the is_unique_email callback function. It never enters the function. All seems to be working fine despite that!
Can anyone help me sorting this issue?
Users Model:
public function form_rules()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('nome', 'Nome', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_is_unique_email');
$this->form_validation->set_rules('password', 'Password', 'required|matches[password_repeat]');
$this->form_validation->set_rules('password_repeat', 'Repeat Password', 'required');
if ($this->form_validation->run() == FALSE)
{
return false;
}
else
{
return true;
}
}
public function is_unique_email()
{
die("asadadaa");
}
My controller
public function insert()
{
if ($this->user_model->form_rules() == FALSE) {
$this->index();
} else {
echo "The form was validated!";
}
}

How to write own function to check if two fields are same using form_validation in codeigniter?

I am using codeigniter and using 'form_validator' to validate posted data from a form. I am come across a situation where I need to check if 2 fields are having same values. There is already a functionality available
$this->form_validation->set_rules( 'new_password', 'New Password', 'trim|required|matches[cpassword]|md5' );
I want to know where the function is written for matches OR where I can define a new function say exactly instead of using matches?
It is defined in system/libraries/form_validation.php
but i would recommend that instead of making changes in the library, you better extend it!
You can create your own "Callbacks" functions
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks
example from user guide...
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_myown_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function myown_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('myown_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}

How to set custom filter in form validation in CodeIgniter?

I am using CodeIgniter's form validation. Here is an example:
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
The documentation say:
Any native PHP function that accepts one parameter can be used as a
rule, like htmlspecialchars, trim, MD5, etc.
What if I want the value to pass through my own custom filter? For example, I would like the value to be cleaned of "badWord".
function curseWordRemove($original = '') {
return str_replace('badWord', '', $original);
}
CodeIgniter already provides ways to do custom validation, but not custom filters. The custom validation only returns true or false, not the filtered string.
function isPolite($string = '') {
if (strpos($string, 'badWord') !== false) {
$this->form_validation->set_message(
'fieldName',
'contains a very bad word'
);
return false;
} else {
return true;
}
}
Jojo, you must have missed it the userguide, its called a callback, and here is the documentation.
An Example:
<?php
class Form extends CI_Controller {
public 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|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public 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;
}
}
}
?>
Basically, create a validation called callback_check_bad_words and a matching function in your controller called check_bad_words($value). Return a boolean as a result (as the result goes back to the validation).
Since you can only pass back a boolean, you need to either use a global variable, OR run the 'sanitization' of your word later on, you don't need it in validation UNLESS you want to stop it from submission.
If your intent is to sanitize the input for bad words, just do it, don't validate for it.

codeigniter set_rules allow more characters

I am building a registration system and i have come to a problem.
I want to allow dots(.) into my username but i cant find a way to do this...
This is just an example which i may want to use in order features of my app as well.
This is what i got so far:
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
Thanks
You'll have to create your own custom function in your controller:
class Form extends CI_Controller
{
function index()
{
$this->load->library('form_validation');
// 'callback_valid_username' will call your controller method `valid_username`
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_valid_username|min_length[6]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
// Validation failed
}
else
{
// Validation passed
}
}
function valid_username($str)
{
if ( ! preg_match('/^[a-zA-Z0-9.]*$/', $str) )
{
// Set the error message:
$this->form_validation->set_message('valid_username', 'The %s field should contain only letters, numbers or periods');
return FALSE;
}
else
{
return TRUE;
}
}
}
For more information on custom validation function, read the docs:
http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

Categories