Unable to set validation rule for alphabetical characters in codeigniter - validation

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

Related

Update data using add action

I'm use Grocery CRUD. I create custom button using add_action.
The button for change data to 0. So, after click the button database update column to 0.
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Attendance extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_builder');
}
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->Attendace_model->update_attendance_status($visitor_id,$attendance_status);
if($update)
{
$this->load->view('Attendance');
}
else
{
alert("error");
}
}
}
Model :
<?php
class Attendance_model extends MY_Model {
function __construct()
{
parent::__construct();
}
function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}
}
What code to use in function for update the data to 0/
Controller:
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->YOUR_MODEL_NAME- >update_attendance_status($visitor_id,$attendance_status);
if($update)
{
return true;
}
else
{
return false;
}
}
Model :
public function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}

Codeigniter & HMVC Callback shows first

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]');
}

Codeigniter function not working when trying to validate user information with database

I am creating a login system and for some reason it redirects back to the login page even after i have put in the correct email and password. The called model function doesnt seem to work. Here is my code:
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run()){
redirect('login/valid_credentials');
}else{
$this->load->view('login_form');
//return false;
}
}
function valid_credentials(){
$this->load->model('login_model');
// oh i see
if ($this->login_model->match_login()){
//return true;
$data = array(
'id'=>$q['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1);
$this->session->set_userdata($data);
$this->load->view('dashboard_view', $data);
}else{
$this->load->view('login_form');
}
}
Model:
class Login_model extends CI_Model{
public function match_login(){
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$q = $this->db->get('user');
if($q->num_rows()== 1){
return true;
}
}
}
View:
<?php
echo "<h1 class ='col-lg-10 col-lg-offset-5'>SIGN IN</h1>";
echo form_open('login/validation', $grid);
echo "<h2>Client</h2>";
"<h3>Please Login</h3>";
echo validation_errors();
echo "<p> Email: </br>";
echo form_input ('email');
echo "</p>";
echo "<p> Password: </br>";
echo form_password ('password');
echo "</p>";
echo "<p>";
echo form_submit ('submit', 'Signin');
echo "</p>";
echo form_close();
?>
For some reason, it is not working, Am not sure what I am missing.
No Need to validate form in one function and check login in another function. use one function for do all.
Changes
Controller code change and optimized
Model code changed
Some References
CodeIgniter Form Validation in FromGet.com (Personally Recommend this site )
Form Validation in Codeigniter.com
Try this
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run() == FALSE )
{
$this->load->view('login_form');
//return false;
}
else{
$this->load->model('login_model');
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->login_model->match_login($email, $password);
if ($result ==false) {
echo "Invalid Cardinals";
}
else
{
$session = array(
'id'=>$result[0]['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1
);
if (!$this->session->set_userdata($session)) {
$data['modelData'] = $result;
$data['sessionData'] = $session;
$this->load->view('dashboard_view', $data);
}
else {
echo "Error in session";.
}
}
}
}
Model
class Login_model extends CI_Model{
public function match_login($email, $password){
$this->db->where('email', $email);
$this->db->where('password', md5($password));
$query = $this->db->get('user');
$result = $query->result_array();
if (empty($result))
{
return false;
}
else
{
return $result;
}
}
}

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...

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