Custom error message using CodeIgniter and Pregmatch Form Validation - php

it is not working, what should i do to make it work ?
if (preg_match('(?=.*[a-z])(?=.*[A-Z]).*', $str))
{
return TRUE;
}
else
{
$this->form_validation->set_message('user_password1', 'Please provide a stronger password');
return FALSE;
}

use your own validation methods some thing like this
<?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('password', 'Password', 'required|callback_password_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function password_check($str)
{
if (preg_match('(?=.*[a-z])(?=.*[A-Z]).*', $str))
{
return TRUE;
}
else
{
$this->form_validation->set_message('password_check' , 'Please provide a stronger password');
return FALSE;
}
}
}

Related

codeigniter status code 400 bad request in index_post() method

I keep on getting this error when performing post and put operations in my codeigniter restful api app.
here is the controller:
public function index_post(){
if(!$this->input->post('city'))
{
$this->response(null,400);
}
$id=$this->cities_model->save($this->input->post('city'));
if(!is_null($id))
{
$this->response(array('response'=> $id),200);
}
else
{
$this->response(array('error'=> 'sorry, data could not be saved...'),400);
}
}
public function index_put(){
if(!$this->input->post('city') || !$id)
{
$this->response(null,400);
}
$update=$this->cities_model->update($id,$this->input->post('city'));
if(!is_null($update))
{
$this->response(array('response' => 'content updated successfully'),200);
}
else
{
$this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), 400);
}
}
There are few improvements and solution as per below.
public function index_post(){
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$id=$this->cities_model->save($this->post('city'));
if(!is_null($id))
{
$this->response(array('response'=> $id),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_put(){
// for put you need to pass id as parameter
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('id','ID','trim|required|integer');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$update=$this->cities_model->update($this->post('id'),$this->post('city'));
if(!is_null($update))
{
$this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
}
}
Now you need to pass city parameter from postman in POST body. it invoke index_post and you pass city and id parameter in PUT body,it invoke index_put.
class Cities_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->set($this->setCity($city))->insert('cities');
if($this->db->affected_rows()>0)
{
return $this->db->insert_id;
}
return null;
}
public function update($id,$city)
{
$this->db->set($this->setCity($city))->where('id',$id)->update('cities');
if($this->db->affected_rows()===1)
{
return true;
}
return false;
}
private function setCity($city)
{
return array('id'=>$city['id'],
'name'=>$city['name']
);
}
}

Callback Function Error ( Unable to access an error message corresponding to your field name )

I am new to Codeigniter. I have error when i try to check the email is existed or not. I saw lot of post on Stackoverflow and other website. I can't get any result.
When i try with below coding i got below errors
Unable to access an error message corresponding to your field name
Email.(email_check)
Please check my code.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}
MODEL
<?php
class Model_users extends CI_Model
{
public function can_log_in()
{
$this->db->where->('email',$this->input->post('email'));
$this->db->where->('password',md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}
?>
I believe you are missing your callback function email_check, and there the set_message should correspond to the function and not the field itself.
You will have to add the 2 callback functions that you are using :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function email_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
public function password_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('password_check', 'Incorrect Username/Password');
return false;
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}

Codeigniter need to connect from one module to another

I want to connect from one controller in one module to another controller in another module. I want to go from my login module to my dashboard module and use a function inside the dashboard module. Just basically switch from my login module to my dashboard module. Here is my login controllers and my dashboard controller.
class Login extends MX_Controller {
function index()
{
$this->load->view('includes/header');
$this->load->view('login_form');
$this->load->view('includes/footer');
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
//$data['main_content'] = 'signup_form';
//$this->load->view('includes/template', $data);
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_if_email_exists');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[15]|callback_check_if_username_exists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE) // didn't validate
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['account created'] = 'Your account has been created. <br /> <br /> You may now login';
$this->load->view('includes/header');
$this->load->view('signup_successful', $data);
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
}
}
function check_if_username_exists($requested_username)
{
$this->load->model('membership_model');
$username_available = $this->membership_model->check_if_username_exists($requested_username);
if ($username_available)
{
return TRUE;
}else{
return FALSE;
}
}
function check_if_email_exists($requested_email)
{
$this->load->model('membership_model');
$email_available= $this->membership_model->check_if_email_exists($requested_email);
if ($email_available)
{
return TRUE;
}else{
return FALSE;
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
my second login controller
class Site extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->is_logged_in();
$this->lang->load('login/login/', 'english');
}
function members_area()
{
$this->load->view('logged_in_area');
//$this->load->module('dashboard/dashboard');
//$this->load->view('home');
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
//$this->load->view('login_form');
}
}
}
the controller in my dashboard module has a function called home I am trying to connect to and use. And the home function of the dashboard has a connection to another module but I cannot get the connection from login to dashboard to work. Also the way my login works I need to connect to the dashboard module through my members_area function. All help greatly appreciated.
Assuming you're using this library
From controller you can use Modules::load or $this->load-module
$moduleInstance = $this->load->module('yourmodule');
// or
$moduleInstance = Modules::load('yourmodule');
// Now you can call any public module controller method
$moduleInstance->somePublicMethod($arg1, $argn);
// you can also use $this->yourmodule as a model instance
Hope this helps

Codeigniter Login Authentication

I need help in the validations of my login page and authentication of the login page also.
Controller:
class User_authentication extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('cookie');
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->database();
}
function index() {
$this->session->set_userdata('message', '');
}
function loadLogin() {
$this->load->view('index.php');
}
function authenticateUser() {
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('pass', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_message('required', 'Invalid Password.');
if($this->form_validation->run() == FALSE) {
$this->load->view('index.php');
}
else {
redirect('homepage/loadhomeView');
}
}
function username_check($str) {
$username = $this->input->get_post('username');
if($str == '' || $str != $username) {
$this->form_validation->set_message('username_check', 'The username is invalid');
return FALSE;
}
else {
return TRUE;
}
}
function logout() {
$this->session->sess_destroy();
$this->load->view('index.php');
}
}
and Model:
class User_model extends CI_Model {
function login_user() {
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->query->get();
if($query->num_rows() == 1) {
return $query->result();
}
else {
return false;
}
}
}
kindly guide me in doing this.. thank you

Object not found on codeigniter

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;
}

Categories