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!
Related
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!";
}
}
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;
}
}
}
I have a login function in CodeIgniter with this code:
public function login() {
$this->redirect_if_logged($this->login_check());
$this->data['active'] = 'login';
$this->load->model('user_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback__validate_login');
if (!$this->form_validation->run()) {
$this->load->template('login', $this->data);
} else {
redirect('/','refresh');
}
}
And a validation function:
public function _validate_login($password) {
$this->form_validation->set_message('_validate_login', 'Invalid username or password');
return false;
}
The problem is that the custom function is never called, the validator always return true if all rules pass. The validator itself works, I checked it with other rules. It is just ignoring my custom function. What am I missing here?
create MY_Form_validation extends CI_Form_validation in your library
class MY_Form_validation extends CI_Form_validation
{
public function _validate_login($password) {
$this->form_validation->set_message('_validate_login', 'Invalid username or password');
return false;
}
in your controller remove callback
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|_validate_login')
i made a controller name of user.php i test complete project on local host it works fine but when i upload online on website it shows error , i submit the form then form load controller called user.php and error on the first line its called parse error i check but still the same will you please suggest.
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_signup');
}
else
{
$this->load->model('Users_model');
if($this->input->post("language")=="ar")
{
$this->load->view('ar_thanks');
}
else
{
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member())
{
$this->load->view('ar_thanks');
}
}
}
}
Please format your code and use IDE. And you will not have these errors. Your formatted code:
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->load->view('ar_signup');
} else {
$this->load->model('Users_model');
if($this->input->post("language")=="ar") {
$this->load->view('ar_thanks');
} else {
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member()) {
$this->load->view('ar_thanks');
}
}
}
}
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.