Codeigniter & HMVC Callback shows first - codeigniter

On my codeigniter Version 3.1.4 / hmvc login if the form validation password input is empty for some reason the callback function shows first rather than the required message?
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
As shown on message it should show the password required message first rather than callback
Question how can I make sure it shows the correct validation messages
first.
Autoloaded form_validation
$autoload['libraries'] = array('database', 'form_validation');
Controller
<?php
class Login extends MX_Controller {
public $CI;
public function __construct() {
parent::__construct();
$this->load->model('catalog/user/user_model');
$this->form_validation->CI =& $this;
}
public function index() {
$data['type'] = $this->input->post('type');
$data['password'] = $this->input->post('password');
$this->form_validation->set_rules('type', 'Username Or Email', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');
if ($this->form_validation->run() == false) {
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$this->load->view('template/account/login', $data);
} else {
$user_info = $this->user_model->get_user($user_id = '', $this->input->post('type', true));
if ($user_info) {
$remember = $this->input->post('remember') ? TRUE : FALSE;
$this->user->login($user_info['user_id'], $remember);
redirect(base_url("users") .'/'. $user_info['user_id'] .'/'. $user_info['username']);
}
}
}
public function validatePassword($str) {
if ($this->user_model->validate_password($str, $this->input->post('type', true))) {
return TRUE;
} else {
$this->form_validation->set_message('validatePassword', 'Opp\'s somthing wrong with login!');
return FALSE;
}
}
}
application > libraries > MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
public $CI;
}

Instead of this -
$this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');
kindly use the callback function like this -
$this->form_validation->set_rules('password', 'password', 'required', 'callback_validatePassword');

just keep the callback validation separate
$this->form_validation->set_rules('password', 'password', 'trim|required');
// check for post values is empty or not?
if(!empty($data['password'])) {
$this->form_validation->set_rules('password', 'password', 'callback_validatePassword[password]');
}

Related

Unable to set validation rule for alphabetical characters in codeigniter

I am using Codeigniter 3, and I referred the various stackoverflow links having similar problem like mines. But I was not able to solve this issue. The callback function customAlpha is called from the file->MY_Form_validation.php, which is currently located in the libraries folder of my project. I changed the regular expression several times,but no use. Please help.
Controller->Login_c.php
<?php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'trim|xss_clean|required|min_length[4]|callback_customAlpha',
array(
'required' => 'Please provide %s.',
'customAlpha' => 'Only characters are allowed in Full Name field'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
if ($this->form_validation->run() == FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>
MY_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
parent::__construct($rules);
}
public function customAlpha($str)
{
//Validation for alphabetical characters and spaces
if ( !preg_match('/^(?:[A-Za-z]+(?:\s[A-Za-z]+|$)){1}$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
?>
I finally was able to solve this issue on my own. Below is the solution that worked.
MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
public function customAlpha($str)
{
if ( !preg_match('/^([a-z]+(-| )?)+$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
Login_c.php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'required|min_length[4]',
array(
'required' => 'Please provide %s.'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
$validation = $this->form_validation->customAlpha($this->input->post('uname'));
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
else if ($this->form_validation->run()== FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else if($validation == false)
{
$errors['err'] = "Error in name field";
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>

Not able to load model in Codeigniter

I have a model called register_model.php, which I have loaded in a function in my controller (register_controller.php). Model file is placed into model folder itself. Still, I get this error.
An Error Was Encountered
Unable to locate the model you have specified: register_model
register_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
// function index()
// {
//
// }
public function register() {
if ($this->session->userdata('logged_in'))
{
//user is already logged in
redirect('index.php');
}
else {
//init
//$data['country_list']=$this->config->item('um_country_list');
$data['username'] = '';
$data['firstname'] = '';
$data['lastname'] = '';
$data['email'] = '';
// $data['password'] = '';
//$data['userlevel'] = '';
//load rules
$rules = $this->config->item('um_register_rules');
//default msg
$data['msg'] = $this->lang->line('um_form_msg');
$this->load->model('register_model');
if (isset($_POST['submit'])) {
//the user has submitted the form
//get the user input
$data['username'] = $this->input->post('username');
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
//$data['userlevel'] = $this->input->post('userlevel');
$this->form_validation->set_rules($rules); //check with the rules
if ($this->form_validation->run() === FALSE) {
//validation failed
$data['msg'] = $this->lang->line('um_form_error');
$this->load->view('user_register_form', $data);
} else {
//validation passed
$dbdata = array(
'username' => $this->input->post('username'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
//'userlevel' => $this->input->post('userlevel')
);
$this->register_model->register_user($dbdata);
$data['msg']=$this->lang->line('um_form_activate');
//render the view
$this->load->view('um_msg', $data);
}
} else {
//render the view
$this->load->view('user_register_form', $data);
}
}
}
}
?>
register_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function register_user($dbdata) {
$this->db->insert('users', $dbdata);
}
}
?>
Probably you should include your model inside your controller, try to include after parent::__construct
This should look like so:
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
or if you using this model in most controllers you can include it using autoload, go to /config/autoload.php search for $autoload['model'] and add your model inside the suitable array
First of all load your model in your controller ..
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
This will work for you...

Code Igniter - Unable to load requested file after redirect

I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result

codeigniter: call Index Method from another method

I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();

Setting form validation rules in CodeIgniter

I have these issue where I want to set a rules on three input forms of type "text", my rule is that at least one of these three has values (either of the three), I have no idea how to set them in CI, because they are altogether executed when run() is triggered, any on of you guys knows how to set these kind of rules to form validation in CI please do share your knowledge.
You can set your own type of validation functions. It is pretty well documented here, but an excerpt would be:
<?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;
}
}
}
?>
callback_username_check is calling the username_check function in the controller
To answer your latest comment
// $data is $_POST
function my_form_validator($data)
{
$data = 'dont worry about this';
// you have access to $_POST here
$field1 = $_POST['field1'];
if($field1 OR $field2 OR $field3)
{
// your fields have value
return TRUE;
}
else
{
// your fields dont have any value
$this->form_validation->set_message('field1', 'At least one of the 3 fields should have a value');
return FALSE;
}
}

Resources