I have a problem in using my call back for form validation.
The validation should check the tbl_fees_type if there is an existing fees_type_name before creating a new fee type, if it exist already it will show an error The Fees Type already exists.
I know the required form validation is working because it shows it is required but when it comes to callback validation that checks the information from the database if it exists using callback its not working.
This is my code:
So I have a Feestype controller like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Feestype extends MX_Controller {
public function __construct() {
parent::__construct();
// loading the fees type model
$this->load->model('model_feestype');
// loading the form validation library
$this->load->library('form_validation');
}
public function index() {
$this->load->view('feestype');
}
public function create()
{
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'fees_type_name',
'label' => 'Fees Type Name',
'rules' => 'trim|required|callback_validate_feestypename'
//this is the callback
),
array(
'field' => 'fees_type_role',
'label' => 'Fees Type Role',
'rules' => 'trim|required|'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if($this->form_validation->run() === true) {
$create = $this->model_feestype->create();
if($create === true) {
$validator['success'] = true;
$validator['messages'] = "Successfully added";
}
else {
$validator['success'] = false;
$validator['messages'] = "Error while inserting the information into the database";
}
}
else {
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
} // /else
echo json_encode($validator);
}
// call back validation function to do
public function validate_feestypename()
{
$validate = $this->model_feestype->validate_feestypename();
if($validate === true) {
$this->form_validation->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
?>
and this is the model_feestype.php Model
<?php if (!defined ('BASEPATH')) exit ('No direct script access allowed');
class Model_Feestype extends CI_Model {
public function __construct() {
parent:: __construct();
}
public function create()
{
$insert_data = array(
'fees_type_name' => $this->input->post('fees_type_name'),
'fees_type_role' => $this->input->post('fees_type_role')
);
$status = $this->db->insert('tbl_fees_type', $insert_data);
return ($status === true ? true : false);
}
public function validate_feestypename()
{
$feestypeName = $this->input->post('fees_type_name');
$sql = "SELECT * FROM tbl_fees_type WHERE fees_type_name = ?";
$query = $this->db->query($sql, array($feestypeName));
return ($query->num_rows() == 1 ? true : false);
}
}
?>
and this is my modal form view php file.
<div class="modal fade" tabindex="-1" role="dialog" id="addFeetype">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add Fees Type</h4>
</div>
<form method="post" action="feestype/create" id="createFeetypeForm">
<div class="modal-body">
<div class="form-group">
<label for="fees_type_name">Fees Type Name</label>
<input type="text" class="form-control" id="fees_type_name" name="fees_type_name" placeholder="Fees Type Name">
</div>
<div class="form-group">
<label for="fees_type_name">Fees Type Role</label>
<select class="form-control" name="fees_type_role" id="fees_type_role">
<option></option>
<option>School Fees</option>
<option>Personal Fees</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
The form required validation is working that looks like this.
Form validation required working
this is the sample form that I want to achieve, it has the same source code but it runs in codeigniter (not HMVC) but it's not working in my work (HMVC).
Woking callback validation should look like this
All you need to do to make callbacks work in HMVC is
MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
Then in the run part add $this in run($this)
$this->form_validation->run($this)
In the folder libraries create class MY_Form_validation
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
// call back validation function to do
public function validate_feestypename()
{
$this->CI =& get_instance();
$this->CI->load->model('model_feestype');
$validate = $this->CI->model_feestype->validate_feestypename();
if($validate === true) {
$this->CI->form_validation->set_message->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
Related
I'm new to codeigniter, I have tried everything but my form is still not submitting to the database.
here's my code. I seriously can't find where I went wrong anymore.
VIEW
<?= form_open('forms/submit_shifter_form');?>
<div id="form-interview-fill-mainform" class="form-group">
<div class="container">
<div class="col-sm-12">
<div class="input-group">
<label>Reason/s for shifting:</label>
<input type="text" class="form-control form-input" name="shifter_reason">
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Strengths:</label>
<input type="text" class="form-control form-input" name="shifter_strength">
</div>
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Weaknesses:</label>
<input type="text" class="form-control form-input" name="shifter_weakness">
</div>
</div>
</div>
</div>
<?php echo form_submit('submit','Confirm', 'class="btn btn-success btn-lg" id="submit"');?>
<?php echo form_close();?>
CONTROLLER
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms extends CI_Controller {
public function session(){
if($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$data['username'] = $session_data['username'];
$data['password'] = $session_data['password'];
return $data;
}
}
public function submit_shifter_form(){
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$this->load->model('forms_model');
$this->forms_model->shifter_form_submit($id,$shifter);
redirect(site_url('profile'), 'refresh');
}
}
MODEL
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms_Model extends CI_Model{
function shifter_form_submit($id,$shifter)
{
$this->db->insert('tbl_prototype_shifter',$shifter);
$insert_id = $this->db->insert_id();
$shift = array(
'studentId' => $id
);
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}
}
My other forms submit data properly in my database, after I added this new form, I seemed like it's not submitting properly anymore. Please, can someone help, I cannot figure out where I went wrong
You got
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
But your form has,
shifter_reason
shifter_strength
shifter_weakness
So rest all fields will be null, is your table accepting null for those remaining fields ?
If no then set some default value,
In controller
Make sure you loaded database:
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('forms_model');
$this->load->helper('form');
}
and in your other method,
$session_data = $this->session->userdata('logged_in');
$this->forms_model->shifter_form_submit($session_data['id'],$shifter);
In model,
function shifter_form_submit($id,$shifter)
{
// try to insert
$this->db->insert('tbl_prototype_shifter',$shifter);
// check if insert was ok
if($this->db->affected_rows() > 0)
{
// if ok get last insert id
$insert_id = $this->db->insert_id();
// data to be updated
$shift = array(
'studentId' => $id
);
// update
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}else{
// failed to insert, so cannot update other table too
echo $this->db->_error_message();
throw new Exception("Can't insert the resource");
}
}
Try this insted of your submitting button.
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.
The data still insert to database despite the same name.
This is the controller code
public function add(){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('add_pbk_g', $data);
}
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));
$this->load->model('mpbk_grup');
if($this->mpbk_grup->check_grup_exist('$Name')){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('layout/header', $title);
$this->load->view('add_pbk_g');
$this->load->view('layout/footer');
} else{
$this->mpbk_grup->add;
redirect('cpbk_grup/index');
}
}
}
this is the Model..
function add($data){
return $this->db->create('pbk_groups', $data);
}
function check_grup_exist($namegroup){
$this->db->where('Name', $namegroup);
$this->db->from('pbk_groups');
$query = $this->db->get();
if($query->num_rows() >0){
return $query->result();
} else {
return $query->result();
//return False;
}
}
and this is the view
<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
<div class="box-body">
<?php echo validation_errors(); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Group</label>
<div class="col-sm-10">
<input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
Kembali
</div>
<!-- /.box-footer -->
</form>
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$data = array ('Name' => $this->input->post('namegroup'));
$this->mpbk_grup->check_grup_exist($data);
}
}
Where does $Name on this line come from?
if($this->mpbk_grup->check_grup_exist('$Name')){
Also it should not have quotes around it, should be...
if($this->mpbk_grup->check_grup_exist($Name)){
Try doing this inside save function
Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);
My Model
Model_Name
$this->main_table = 'pbk_groups';
public function check_grup_exist($data)
{
$query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0)
{
$this->db->insert($this->main_table, $data);
return $this->db->insert_id();
}
else
{
return false;
}
Checking duplication data before inserting the data. Let me know if any query occurs.
I have tried to copy from Codegniter's documentation, but I can't make form validation callbacks working.
I added helper form, url and library form_validation. It's not working and always returns "false"
Controller
public function addtest()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($this->form_validation->run() == TRUE)
{
die('true');
}
else
{
die('false');
}
}
$this->template
->build('myform',array());
}
public function username_check($str)
{
if ($str == 'test')
{
return TRUE;
}
else
{
return FALSE;
}
}
View
<form method="post" action="" class="form-horizontal form-label-left">
<div class="col-xs-12 col-md-9">
<div class="x_panel">
<div class="form-group col-xs-12">
<div class="col-xs-3">
<label class="control-label">Folder name</label>
</div>
<div class="col-xs-9">
<input type="text" name="username" value="" class="form-control " id="" placeholder="">
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Extend your form_validation library in Libraries.php
MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI =& $module;
return parent::run($group);
}
}
/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */
My Controller function is like this and it runs perfectly. I have autoloaded all libraries
public function change_password()
{
if($this->isLoggedin()){
$data['title']='Change Password';
if($_POST)
{
$config=array(
array(
'field' => 'old_password',
'label' => 'Old Password',
'rules' => 'trim|required|callback_checkPassword'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
),
array(
'field' => 'conf_password',
'label' => 'Confirm Password',
'rules' => 'trim|required|matches[password]'
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == false)
{
// if validation has errors, save those errors in variable and send it to view
$data['errors'] = validation_errors();
$this->load->view('change_password',$data);
}
else
{
// if validation passes, check for user credentials from database
$this->Login_model->updatePassword($_POST['password'],$this->session->userdata['id']);
$this->session->set_flashdata('log_success','Congratulations! Password Changed');
redirect(base_url() . 'Login/dashboard');
}
}
else
{
$this->load->view('change_password',$data);
}
}
else
{
redirect(base_url().'Login');
}
}
public function checkPassword($str)
{
$check=$this->Login_model->checkPassword($str);
if($check)
{
return true;
}
else
{
$this->form_validation->set_message('checkPassword', 'The Current Password you have provided is incorrect');
return false;
}
}
In HTML (Add ID field)
<input type="text" name="username" value="" class="form-control " id="username" placeholder="">
<button type="submit" class="btn btn-success" id="submit">Submit</button>
In your AJAX code
<script type="text/javascript">
$(function(){
$( "#submit" ).click(function(event)
{
event.preventDefault();
var username= $("#username").username();
$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/controller/Method",
data:{ username:username},
success:function(res)
{
}
});
});
});
</script>>
In Controller
No need of check if($_SERVER['REQUEST_METHOD'] == 'POST') because it comes through it always
public function addtest()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
if ($this->form_validation->run() == TRUE)
{
die('true');
}
else
{
die('false');
}
$this->template->build('myform',array());
}
public function username_check($str)
{
if (empty($str))
{
echo "Empty";
}
else
{
if ($str == 'test')
{
return TRUE;
}
else
{
return FALSE;
}
}
}
Check more about CodeIgniter callback function
I want a login solution in CodeIgniter. I want code for user login, (the user who is registered) I'm using Mysql with php and working on localhost
Kindly tell me that how can I make a login controller and model for login
I'm attaching my register.php so may be you understand what should I code for login.
I updated my question.
Here I'm attaching my updated files
login.php (controller)
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller {
function index() {
$myData['pageName'] = 'login';
$this->load->helper(array('form', 'url'));
$this->load->database();
if ($_POST) {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// Login page view...
$this->load->view('login', $myData);
}
else{
}
$this->load->model('Login');
if ($this->Login->check_login_details($this->input->post('email') , $this->input->post('password'))) {
// show success
$this->load->view('success_login', $myData);
}
}else {
// Invalid details...
$this->load->view('login', $myData);
}
}
}
?>
Core Model where i add function check_details
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Model extends CI_Model {
const TableName = 'form';
const TablePK = 'id';
public function ins_record() {
$this->db->insert($this::TableName, $this);
}
public function getRec() {
$res = $this->db->get($this::TableName);
return $res->result_array();
}
public function get_userName($colName, $value) {
$res = $this->db->get_where($this::TableName, array($colName => $value));
return $res->result_array();
}
public function check_login_details($email, $password)
{
// Do whatever to the password...
$password = md5($password);
$query = $this->db->get_where($this::TableName, array('email' => $email, 'password' => $password));
return ($query->num_rows() > 0) ? true : false;
}
}
?>
this is Model of login.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends My_Model{
const TableName = 'form';
const TablePK = 'id';
public $email = '';
public $password = '';
}
?>
here is also my login.php (view code)
<?php
include('./includes/header.php');
?>
<!-- MAIN -->
<div id="main">
<!-- wrapper-main -->
<div class="wrapper">
<!-- content -->
<div id="content">
<!-- title -->
<div id="page-title">
<span class="title">Sign in Form</span>
<span class="subtitle">Kindly login here to access site's useful features</span>
</div>
<!-- ENDS title -->
<form class="form-signin" role="form" method="post">
<h2 >Please sign in</h2>
<input type="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" class="form-control" placeholder="Password" required >
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<!-- ENDS content -->
</div>
<!-- ENDS wrapper-main -->
</div>
<!-- ENDS MAIN -->
<!-- Twitter -->
<div id="twitter">
<div class="wrapper">
<img id="bird" src="img/bird.png" alt="Tweets" />
<div id="tweets">
<ul class="tweet_list"></ul>
</div>
</div>
</div>
<!-- ENDS Twitter -->
<?php
include('./includes/footer.php');
?>
Now please tell me what code should be used for successful login that fetch data from database
Creating a login form, isn't too difficult. All you have to do is check the inputted values against the values in your database. This should help you;
This would be your controller function your login form would send the values to;
function login()
{
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ( $this->form_validation->run() == false )
{
// Login page view...
}
else
{
if ( $this->Users_model->check_login_details($this->input->post('email'), $this->input->post('password')) )
{
// Login was OK....
}
else
{
// Invalid details...
}
}
}
Then, your "check_login_details" function would be something like this;
function check_login_details($email, $password)
{
// Do whatever to the password...
$password = md5($password);
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password));
return ($query->num_rows() > 0) ? true : false;
}
This would return true to the controller, which would then allow you to set the session and redirect the user to wherever you like...
Hope this helps.
I'm using codeigniter for a login form validation. The code was previously working fine, but when I made some modifications, it just do not work now.
When I try to type in wrong or leave blank for password or username, the error message is shown as normal, but when I type in the correct username and password, $this->form_validation->run() just give me FALSE with an empty validation_errors() string.
I've tried to restore my old working controller php, but this error resists.
Full code related is available at
User Controller http://pastebin.com/nt2SDVnv
Login View http://pastebin.com/9hEc0EJB
User Model http://pastebin.com/p1z0zLM8
Only related code are pasted below:
Controller:
<?php
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('cookie');
}
function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
$this->_username = $this->input->post('username'); //用户名
$remember_me = $this->input->post('remember_me');
$json_string = $this->input->cookie('userinfo');
$userinfo_json = json_decode($json_string);
if(isset($userinfo_json->username)){
if ($this->username_check($userinfo_json->username)){
$this->user_model->login($userinfo);
redirect('admin/dashboard');
}
}
if ($this->form_validation->run() == FALSE){
$this->load->view('account/login');
} else {
$userinfo=$this->user_model->get_by_username($this->_username);
$this->user_model->login($userinfo);
if($remember_me=="on"){$this->user_model->write_session($userinfo);}
redirect('admin/dashboard');
}
}
function username_check($username){
if ($this->user_model->get_by_username($username)){
return TRUE;
}else{
$this->form_validation->set_message('username_check', 'User name not exist.');
return FALSE;
}
}
function password_check($password) {
$password = md5($password);
if ($this->user_model->password_check($this->_username, $password)){
return TRUE;
}else{
$this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
return FALSE;
}
}
}
View:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login - <?=$this->admin_model->get_title();?></title>
<?php $this->load->view('gy/head');?>
</head>
<body>
<?php $this->load->view('gy/header');?>
<div class="hero-unit header">
<div class="container">
<div style="text-align:center;">
<h1>Sign in</h1>
<p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
</div>
</div>
</div>
<div class="container">
<div class="span5 offset3">
<?php if(validation_errors() !== '' || #(!$err_message == '')){ ?>
<div class="alert alert-error fade in">
×
<strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=#$err_message?>
</div>
<?php } ?>
<?php if(#$message!=''){ ?>
<div class="alert fade in alert-success">
×
<strong>Success!</strong> <?=$message?>
</div>
<?php } ?>
<?php echo form_open('login',array('class'=>'form-horizontal')); ?>
<div class="control-group">
<label class="control-label" for="username">User name</label>
<div class="controls">
<input type="text" id="username" name="username" placeholder="User name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password">
</div>
</div>
<label for="remember_me" class="checkbox">
<input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
</label>
<div style="text-align:center;">
<input type="submit" name="submit" value="Log in" class="btn btn-primary">
</div>
</Form>
</div>
</div>
<?php $this->load->view('gy/footer');?>
</body>
</html>
Model:
<?php
class user_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
}
function login($userinfo)
{
$data = array('username'=>$userinfo->username,
'user_id'=>$userinfo->id,
'role'=>$userinfo->role,
'logged_in'=>TRUE);
$this->session->set_userdata($data);
}
function write_session($userinfo)
{
$user_json = json_encode($userinfo);
$cookie = array(
'name' => 'userinfo',
'value' => $user_json,
'expire' => '2592000',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
}
function get_by_username($username)
{
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() == 1)
{
return $query->row();
}
else
{
return FALSE;
}
}
function password_check($username, $password)
{
if($user = $this->get_by_username($username))
{
return $user->password == $password ? TRUE : FALSE;
}
return FALSE;
}
}
I suspect the trim rule. It doesn't return a Boolean and I suppose you haven't set any error message for that rule. trim() should be run on the username and password, after the validation runs successful. If you just wanna check if the input has blank spaces You could add a rule to check with the strpos() but again in a custom callback. Just remember to use the Identical opperator '===' instead of the equal '==' .
Controller :
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');
function space_check($input){
if(strpos(' ',$input)===false){
return TRUE;
}else{
$this->form_validation->set_message('space_check', '%s contains space.');
return FALSE;
}
}