I am facing this problem from yesterday and can not fix it. My callback function is not working. It's always returning TRUE, but i don't know why? Can anyone help me?
Here is the the model:
class Login_model extends CI_Model {
public function check_login($str)
{
$this->form_validation->set_message('check_login', 'Error');
return FALSE;
}
function validate_login()
{
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'callback_check_login');
if($this->form_validation->run() == FALSE)
{
return FALSE;
}
return TRUE;
}}
The callback function should never return TRUE. But it's returning! I am going to die with this problem! :#
It will return TRUE because
You are not passing email and password to 'validate_login()'.
So $this->form_validation->run() will not work.
move validate_login() to any controller
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 have codeigniter installed on my localhost
The main.php controller is
class Main extends CI_Controller {
public function index()
{
$this -> login();
}
public function login()
{
$this->load->view('login');
}
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
The login is working page is coming,but after I fill the username and password,should take me to login/main/login_validation and from there the function login_validation() should either redirect to main/members or show me the login page.But what happens is when I submit the form,the object not found error is coming.Can anyone help me out?
form is
form_open('main/login_validation');
To answer what I think your question is getting at, you're using the form_validation class wrong. You should be testing whether or not the form_validation->run() is TRUE (all fields passed validation) or FALSE (there was an error in one or more of the inputs). Structure you code like such:
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('login');
}
else
{
redirect('main/members');
}
}
This is covered in the CodeIgniter documentation here.
in your controller ..you could try this..
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
$this->check_database();
//redirect('home');
}
}
function check_database()
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
$password = $this->input->post('password');
//query the database
$query_result = $this->model_login->login($username, $password);
if($query_result)
{
$this->load->view('main/members');
}
else
{
redirect('login');
}
}
in model
function login($username,$password){
$query = $this->db->query("SELECT * FROM `login` WHERE `username`= '".$username."' AND
password='".$password."' LIMIT 1");
if ($query->num_rows() > 0)
return TRUE;
else
return FALSE;
}
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