callback function is not working in codeigniter - codeigniter

I have used callback function for checking whether the email is active or not:
My controller is not accessing the callback function.
my controller code:
function index() {
//Session redirection
header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0',false);
header('Pragma: no-cache');
$this->session->unset_userdata('created_user_id');
$this->session->unset_userdata('created_user_type');
$this->session->unset_userdata('edit_user_type');
if ($this->input->server('REQUEST_METHOD') === 'POST'){
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
$this->form_validation->set_rules('terms', 'Terms', 'trim|required');
if($this->input->post('email'))
{
$this->form_validation->set_rules('email', 'Email ID', 'callback_email_check');
} echo "out"; die;
$this->form_validation->set_error_delimiters('<div class="error_user" style="color:red">', '</div>');
if($this->form_validation->run()){
if($this->input->post('Join')!="") {
$this->session->set_userdata('join_email', $this->input->post('email'));
redirect(base_url().'caregivers/my-account');
}
}
}
$this->load->view('site/landing-page');
}
My callback function code:
public function email_check(){
$get_status = $this->web_user_model->get_status($this->input->post('email'));
if($get_status['is_active']=='1')
{
$this->form_validation->set_message('email_check','Email ID already Exits');
return FALSE;
}else{
return TRUE;
}
}
I dont know where i have done the mistake.
Can anyone help me???
thanks in advance..

You set twice your validation rule :
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
....
if($this->input->post('email'))
{
$this->form_validation->set_rules('email', 'Email ID', 'callback_email_check');
}
this is useless because you already set your email field to required so the if is already checked by the validation rule.
Try :
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|callback_email_check');
Also your callback function should expect one parameter
public function email_check($email)
{
$get_status = $this->web_user_model->get_status($email);
...

Related

Restriction for not allowing same email and password in the database for my signup form

i want to make a restriction in my signup form which the user cant signup with the already exists email and password..
in my controller:
i already make a rules or callback
array(
'field' => 'txt_email',
'label' => 'Email',
'rules' => 'required|valid_email|callback_check_if_valid_email|trim',
),
check_if_valid:
public function check_if_valid_email()
{
$where = array('email' =>$this->input->post('txt_email'),'password' =>$this->input->post('txt_password'));
$this->load->model('database_model');
if ($user = $this->database_model->validate_user('user', $where))
{
foreach ($user as $row) {
$checkemail = $row->email;
$checkpassword = $row->password;
}
if ($checkemail == $this->input->post('txt_email')){
$this->form_validation->set_message('check_if_valid_email', 'Email already existed!');
return false;
}
else
{
if ($checkpassword = $this->input->post('txt_password'))
{
$this->form_validation->set_message('check_if_valid_email', 'Email already exists!');
return false;
}
else
{
return true;
}
}
}
}
in my model:
public function validate_user($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
CodeIgniter's Form Validation library comes with the rule you're looking for, called: is_unique
http://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference
Your validation rules will look like this;
required|valid_email|is_unique[users.email]|trim
All you have to set it the table and column you want to be unique (users.email).
Hope this helps.

Show error message after a page refresh

I have a login form that I'm doing 2 part validations: The client-side and the server side.
Client-side: check if valid email, check if password is minimal 6 characters, etc. If something goes wrong on the client-side I can show the user the error messages on the same page with the login form, because it page never got reloaded because it will not send a post to the server if the client-side validation isn't true.
But when it is true, then I'm doing a server side validation -> check if email and password matches to effectively log in into the website. But if the login credentials aren't matching, I need to show a error message. This is the part where I'm stuck. Where and how do I get a message on the same page (login form) because page gets posted and refreshed so I'm losing data. Now when credentials aren't correct I'm redirecting the user back to the login page, but without any messages. But I'm trying to achieve that he'll see the message 'credentials aren't correct'. Can someone help me with this?
Login View
<?php
$loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
$loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
$loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
echo form_open('login/inloggen', array('class' => 'grid-100 formc'));
echo form_input($loginEmail, set_value('loginEmail'));
echo form_password($loginPassword);
echo form_submit($loginSubmit);
echo form_close();
?>
Login Controller
function index(){
$logged_in = $this->logged_in->is_logged_in();
if($logged_in){
$this->load->view('profile_view');
}
else{
$data['content'] = 'login_view';
$this->load->view('templates/template', $data);
}
}
function inloggen(){
if($this->input->post('loginSubmit')){
if($this->form_validation->run('login_validation_rules') == FALSE){
$this->index();
}
else{
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('profile');
}
else{
$this->index();// If credentials aren't correct, redirect them to login page. But how I set a message here?
}
}
}
}
Login Model
function validate(){
$this->db->where('email', $this->input->post('loginEmail'));
$this->db->where('password', md5($this->input->post('loginPassword')));
$query = $this->db->get('tbl_users');
if($query->num_rows == 1){
return true;
}
else{
return false;
}
}
I will prefer do to in one controller, and it will be more clear. I make one simple example, just to tell you the way how I think is good.
function login(){
$data['error_message'] = "";
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
if($this->input->post('loginSubmit')){
// Make rules of validation that need to be required
if($this->form_validation->run('login_validation_rules')){ // If true it goes IN
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('profile'); // Go IN profile
} else {
$data['error_message'] = "Something went wrong"; // This error you write on View
}
}
}
$this->load->view('login_view', $data) //Load view of login
}
I didn't test, and it will need to adapt in your data, but I just want to explain the way. It you have any problem, write an comment, and we will find the solution together.
I have solved this problem like so: Basically when the model sends a false. I make var with a message in it and then in my index I check if that var is there or not.
The only thing I want to know is, is it okay to set a message in my controller or should I have done that somewhere elsewhere??
function index(){
$logged_in = $this->logged_in->is_logged_in();
if($logged_in){
$this->load->view('profile_view');
}
else{
//Check here if my redirection submitted also a message
if(isset($this->ongeldig)){
$data['ongeldig'] = $this->ongeldig;
}
else{
$data['ongeldig'] = '';
}
$data['content'] = 'login_view';
$this->load->view('templates/template', $data);
}
}
function inloggen(){
if($this->input->post('loginSubmit')){
if($this->form_validation->run('login_validation_rules')){
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('profile');
}
else{// set a error message, when the models function returns false.
$this->ongeldig = "Ongeldig wachtwoord/gebruikersnaam";
$this->index();
}
}
else{
$this->index();
}
}
}

Codeigniter : Validating two fields

I have two numeric fields to collect data from users. Need to validate it using codeigniter form validation class.
Conditions:
First field can be zero
Second field cannot be zero
First field should not be equal to second field
Second field should be greater than first field
Currently I use
$this->form_validation->set_rules('first_field', 'First Field',
'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field',
'trim|required|is_natural_no_zero');
But, how to validate for 3rd and 4th condition mentioned above?
Thanks in advance.
Thanks dm03514. I got it working by the below callback function.
$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']');
and the callback function is:
function check_equal_less($second_field,$first_field)
{
if ($second_field <= $first_field)
{
$this->form_validation->set_message('check_equal_less', 'The First &/or Second fields have errors.');
return false;
}
return true;
}
Everything seems to be working fine now :)
You can write your own validation function for 3, and 4 using callbacks
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks
Example from doc
<?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;
}
}
}
?>
If you are using HMVC and the accepted solution doesn't work then
add the following line after initialization in your controller
$this->form_validation->CI =& $this;
So it will be
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
in your controller.

Codeigniter callback validation problems

I want to create a callback function that is used during validation to check if the username / email address is already in the database... problem is I just cant seem to get it working
So this is the callback function:
function callback_username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
And this is the validation logic :
// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}
I have been at this for hours and have no idea what i'm doing wrong... both functions are in the same controller and all other standard validation rules work just fine.
Even when i set the callback function to just return FALSE, it still validates the username.
Any ideas guys... its driving me up the wall at the moment :S
to invoke a callback in CI you don't need to name the function " callback_ my_function" - this it would appear is automatically appended.
this should work:
function username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
// set the rule
$this->form_validation->set_rules('username', 'Username', 'callback_username_available');
// lets do this ~
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
to clarify by calling your function "callback_username_available", CI is attempting to find
callback_callback_username_available() which of course doesn't exist.
// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}

Codeigniter Form validation problem

Please please please can someone help me
$this->load->library('form_validation');
$this->load->helper('cookie');
$data = array();
if($_POST) {
// Set validation rules including additional validation for uniqueness
$this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
$this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
$this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');
// Run the validation and take action
if($this->form_validation->run()) {
echo 'valid;
}
}
else{
echo 'problem';
}
Form validation is coming back with no errors can cany one see why?
Is it actually echoing 'valid'? (you're missing an apostrophe there, btw)
The code you show will only echo 'problem' when $_POST is false, not when validation fails.
Without knowing more, it may be as simple as:
// Run the validation and take action
if($this->form_validation->run()) {
echo('valid');
} else {
echo('invalid');
}
Try like this without checking if $_POST is set - not really needed:
//validation rules here
//
if ($this->form_validation->run() == TRUE) {
//do whatever that shall be run on succeed
} else {
$this->load->view('form'); //load the form
}
Read more about the controller part here

Resources