How can we show error alert using bootstrap alert components in CodeIgniter 3 - codeigniter

I am creating upload, edit and delete in Codeigniter and I want bootstrap alert should display uploading errors(if any) in div alert. Code for the controller is mentioned below, please assist.
code for Controller starts here
<?php
defined('BASEPATH') OR exit ('No direct script access allowed');
/**
*
*/
class Admin_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library("session");
}
public function registerAdminDetails()
{
$config['upload_path'] = 'assets/img/uploads/reg_Admin/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(! $this->upload->do_upload('avatar'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error);
$this->load->view('admin_access/admin_add_new');
}
else
{
$avatar_upload = $this->upload->data();
$data = array(
'admin_name' => $this->input->post('admin_name'),
'admin_username' => $this->input->post('admin_username'),
'admin_email' => $this->input->post('admin_email'),
'admin_password' => $this->input->post('password'),
'admin_profile_pic' => $this->input->post('avatar'),
'admin_profile_pic' => $config['upload_path'].$avatar_upload['file_name']);
$this->db->insert('admin_profile',$data);
echo "<script>alert('New Admin Successfully Enrolled')</script>";
$this->load->view('admin_access/profile/admin_add_new');
}
}
}
?>

$result = $this->add_model->save($fields,array('id' => $id));
if($result)
{
$this->session->set_flashdata('message', array('message' => 'Updated Successfully','class' => 'alert alert-success fade in'));
}
else
{
$this->session->set_flashdata('message', array('message' => 'Something went wrong, please try again','class' => 'alert alert-danger fade in'));
}

Controller :
if($result <= 0) {
$this->session->set_flashdata('error', 'There is error');
} else {
$this->session->set_flashdata('success', 'Yey!');
}
My example view (yes i am using bootstrap) :
<div class="col-sm-12">
<?php
$this->load->helper('form');
$error = $this->session->flashdata('error');
if($error) {
?>
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
<?php
$success = $this->session->flashdata('success');
if($success)
{
?>
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php } ?>
<div class="row">
<div class="col-md-12">
<?php echo validation_errors('<div class="alert alert-danger alert-dismissable">', ' <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>'); ?>
</div>
</div>
</div>

Related

validation errors not net to the flashdata in codeigniter

I'm trying to set validation error to the my form using flashdata in codeigniter but i cannot see the error in a view here is my code of view section. I had already read other relative questions close to my this problem but i'm not found a solution!
<?php if($this->session->flashdata('errors')): ?>
<?php echo $this->session->flashdata('errors');?>
<?php endif; ?>
<?php $attributes = array('id'=>'admin_login','class'=>'form-horizontal'); ?>
<?php echo form_open('admin/login', $attributes); ?>
<div class="form-group">
<?php
$lbl = array('class'=>'col-sm-12 col-lg-12 col-md-12 col-xs-12 control-label');
echo form_label('Email','admin_login',$lbl); ?>
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'form-control col-sm-12 col-lg-12 col-md-12 col-xs-12',
'name'=> 'email',
'placeholder'=>'Enter your email'
); ?>
<?php echo form_input($data); ?>
</div>
</div>
<div class="form-group">
<?php
echo form_label('Password','admin_login',$lbl); ?>
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'form-control',
'name'=> 'password',
'placeholder'=>'Password'
); ?>
<?php echo form_password($data); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'btn btn-success btn-md center mt-2',
'name'=> 'submit',
'placeholder'=>'Password',
'value'=>'LOGIN',
'type'=>'submit'
); ?>
<center>
<?php echo form_submit($data); ?>
</center>
</div>
</div>
<?php echo form_close(); ?>
This is my controller part it is name of 'admin' method name is login
public function login(){
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
// check the form has any error (any miss of rules we set in above codes
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errors',
validation_errors());
// $this->session->set_flashdata('name','Your message');
// $errors = validation_errors();
// $this->session->set_flashdata('form_error', $errors);
// $this->session->set_flashdata('name','Your message');
// $data = array('errors'=> validation_errors());
// set_userdata() is regular way to set session, it need manually unset, there for we use flashdata
// $this->session->set_flashdata($data);
}else{
echo "its all good";
}
}
i tried so many codes, i commented out what i used so far. I set in autoload section session library also $autoload['libraries'] = array('database','form_validation','session');
Please tell me what is the wrong here.
Please try with the below code.
public function login(){
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == true){
echo "its all good";
}else{
$this->session->set_flashdata('errors', validation_errors());
}
$this->load->view('Your View Page');
}

retrive two uploaded images in two separate pages of a same web page

This is my controller with index function and index2 function to upload two images
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Files extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->model('file');
}
function index(){
$data = array();
if($this->input->post('fileSubmit') && !empty($_FILES['userFiles']['name'])){
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$uploadPath = 'uploads/files/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
if(!empty($uploadData)){
//Insert file information into the database
$insert = $this->file->insert($uploadData);
$statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
$this->session->set_flashdata('statusMsg',$statusMsg);
}
}
//Get files data from database
$data['files'] = $this->file->getRows();
//Pass the files data to view
$this->load->view('upload_files', $data);
}
function index2(){
$data = array();
if($this->input->post('fileSubmit2') && !empty($_FILES['userFiles']['name'])){
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$uploadPath = 'uploads/index2/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
$uploadData[$i]['divid'] =2;
}
}
if(!empty($uploadData)){
//Insert file information into the database
$insert = $this->file->insert($uploadData);
$statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
$this->session->set_flashdata('statusMsg',$statusMsg);
}
}
//Get files data from database
$data['files'] = $this->file->getRows2();
//Pass the files data to view
$this->load->view('upload_files', $data);
}
}
This is my model to retrieve images from databases
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Model{
public function getRows($id = ''){
$this->db->select('id,file_name,created');
$this->db->from('files');
if($id){
$this->db->where('divid',1);
$query = $this->db->get();
$result = $query->row_array();
}else{
$this->db->order_by('created','desc');
$query = $this->db->get();
$result = $query->result_array();
}
return !empty($result)?$result:false;
}
public function insert($data = array()){
$insert = $this->db->insert_batch('files',$data);
return $insert?true:false;
}
public function getRows2($id = ''){
$this->db->select('id,file_name,created');
$this->db->from('files');
if($id){
$this->db->where('divid',2);
$query = $this->db->get();
$result = $query->row_array();
}else{
$this->db->order_by('created','desc');
$query = $this->db->get();
$result = $query->result_array();
}
return !empty($result)?$result:false;
}
public function insert2($data = array()){
$insert = $this->db->insert_batch('files',$data);
return $insert?true:false;
}
}
This is my view to upload 2 images and view those 2 images
<div class="container">
<div class="row">
<p><?php echo $this->session->flashdata('statusMsg'); ?></p>
<form enctype="multipart/form-data" action="<?php echo site_url('Upload_Files/index'); ?>" method="post">
<div class="form-group">
<label>Choose Files1</label>
<input type="file" class="form-control" name="userFiles[]" multiple/>
</div>
<div class="form-group">
<input class="form-control" type="submit" name="fileSubmit" value="UPLOAD"/>
</div>
</form>
</div>
<div class="row">
<ul class="gallery">
<?php if(!empty($files)): foreach($files as $file): ?>
<li class="item">
<img src="<?php echo base_url('uploads/files/'.$file['file_name']); ?>" alt="" >
<p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p>
</li>
<?php endforeach; else: ?>
<p>Image(s) not found.....</p>
<?php endif; ?>
</ul>
</div>
</div>
<div class="container">
<div class="row">
<p><?php echo $this->session->flashdata('statusMsg'); ?></p>
<form enctype="multipart/form-data" action="<?php echo site_url('Upload_Files/index2'); ?>" method="post">
<div class="form-group">
<label>Choose Files1</label>
<input type="file" class="form-control" name="userFiles[]" multiple/>
</div>
<div class="form-group">
<input class="form-control" type="submit" name="fileSubmit2" value="UPLOAD"/>
</div>
</form>
</div>
<div class="row">
<ul class="gallery">
<?php if(!empty($files)): foreach($files as $file): ?>
<li class="item">
<img src="<?php echo base_url('uploads/index2/'.$file['file_name']); ?>" alt="" >
<p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p>
</li>
<?php endforeach; else: ?>
<p>Image(s) not found.....</p>
<?php endif; ?>
</ul>
</div>
</div>
My problem is when I upload one image it appears in the other image view also. I tried so hard and put this question 2 times in stackoverflow. If someone helps me It will be a great thing

codeigniter Invalid argument supplied for foreach()

I have a full working code with retrieve insert update and delete data to database.
All is working well, but if I delete some data, it is deleted but when direct to my view page again, then it will return "Invalid argument supplied for foreach()".
When I press again my view page link it is all normal and the data I delete is gone too, this is just happen with delete function.
Code :
Controller :
public function merk(){
//$data["result"] = $this->modelAdminMerk->getMerk();
$data["totalRows"] = $this->modelAdminMerk->getTotalRows();
$config["base_url"] = base_url() . "admin/merk";
$config["per_page"] = 15;
$config["total_rows"] = $data["totalRows"];
$config["uri_segment"] = 3;
$config["num_links"] = 10;
$this->pagination->initialize($config);
if( ! $this->uri->segment(3)){
$page = 0;
}else{
$page = $this->uri->segment(3);
}
$data["results"] = $this->modelAdminMerk->getMerkByPages($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminMerk", $data);
$this->load->view("adminView/adminFooter");
}
public function insertMerk(){
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminInsertMerk");
$this->load->view("adminView/adminFooter");
}
public function validateInsertMerk(){
$this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]");
if($this->form_validation->run()){
$newMerk = array("merk" => $this->input->post("namaMerk"));
$this->modelAdminMerk->insertMerk($newMerk);
$this->merk();
}else{
echo "GAGAL!!!";
}
}
public function editMerk($id){
$data["merk"] = $this->modelAdminMerk->getMerkById($id);
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminEditMerk", $data);
$this->load->view("adminView/adminFooter");
}
function validateEditMerk(){
$this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]");
if($this->form_validation->run()){
$newMerk = array("merk" => $this->input->post("namaMerk"));
$this->modelAdminMerk->editMerk($this->input->post("id"), $newMerk);
$this->merk();
}else{
echo "GAGAL";
}
}
public function deleteMerk($id){
$this->modelAdminMerk->deleteMerk($id);
$this->merk();
}
Model :
function getMerk(){
$data = $this->db->query("SELECT * FROM merkbarang");
return $data->result();
}
function getMerkById($id){
$this->db->where("id", $id);
$data = $this->db->get("merkbarang");
return $data->row();
}
function insertMerk($newMerk){
$this->db->insert("merkbarang", $newMerk);
return true;
}
function editMerk($id, $newMerk){
$this->db->where("id", $id);
$this->db->update("merkbarang", $newMerk);
return true;
}
function deleteMerk($id){
$this->db->where("id", $id);
$this->db->delete("merkbarang");
return true;
}
function getTotalRows(){
$rows = $this->db->count_all("merkbarang");
return $rows;
}
function getMerkByPages($limit, $start){
$this->db->limit($limit, $start);
$query = $this->db->get("merkbarang");
if($query->num_rows() > 0){
foreach ($query->result() as $row){
$data[] = $row;
}
return $data;
}else{
return false;
}
}
View :
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="panel panel-danger">
<div class="panel-body">
<ul class="nav nav-pills nav-stacked">
<li role="presentation" class="active">Insert Merk</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
<strong>Merk</strong>
</div>
<div class="panel-body">
<div class="row" id="tabelBarang">
<div class="col-md-1 colBarang">
<strong>No</strong>
</div>
<div class="col-md-7 colBarang">
<strong>Merk</strong>
</div>
<div class="col-md-4 colBarang">
<strong>Action</strong>
</div>
</div>
<?php
$angka = 0;
foreach ($results as $row){
$id = $row->id;
$merk = $row->merk;
?>
<div class="row" id="tabelBarang">
<div class="col-md-1 colBarang">
<?php echo ++$angka; ?>
</div>
<div class="col-md-7 colBarang">
<?php echo $merk; ?>
</div>
<div class="col-md-2 colBarang">
<button type="button" class="btn btn-info btn-xs" aria-label="Left Align">
Edit
</button>
</div>
<div class="col-md-2 colBarang">
<button type="button" class="btn btn-danger btn-xs" aria-label="Left Align">
Delete
</button>
</div>
</div>
<?php
}
?>
<?php echo $links; ?>
</div>
</div>
</div>
</div>
</div>
The way I see it, there is nothing wrong in your code, that should definitely work, but I would suggest you to try CI's redirect() method in your delete function
public function deleteMerk($id){
$this->modelAdminMerk->deleteMerk($id);
redirect('controllername/merk');
}

Display error message from Admin_Controller to any view

I would like to be able to display data message from my Admin_Controller in the core folder core/Admin_Controller.php to then show up on my login view.
I can only seem to get it working with session flash data but would not like to use flash data.
So what would be best method on getting the $data['error_warning'] message to from my Admin_Controller to be able to work on my Login controller and view.
<?php
class Admin_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user');
Modules::run('admin/error/permission/check');
$ignore = array(
'login',
'logout'
);
if (!in_array($this->router->fetch_class(), $ignore)) {
if ($this->session->userdata('user_id') == FALSE) {
$data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
redirect('admin');
}
}
}
}
Login Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Admin_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$username = $this->input->post('username');
if (isset($username)) {
$data['username'] = $username;
} else {
$data['username'] = '';
}
$password = $this->input->post('password');
if (isset($password)) {
$data['password'] = $password;
} else {
$data['password'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard'.'/'.$token);
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', '<i class="fa fa-exclamation-triangle"></i> Does not match any of our database records!');
return false;
} else {
return true;
}
}
}
Login View
<?php echo Modules::run('admin/common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12">
<div class="panel panel-default" style="margin-top: 12.5%;">
<div class="panel-heading"><strong><i class="fa fa-key"></i> Enter Details To Login </strong></div>
<div class="panel-body">
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php $data = array('class' => 'form-horizontal');?>
<?php echo form_open('admin', $data);?>
<?php if ($error_warning) { ?>
<div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<?php if ($this->session->flashdata('error')) { ?>
<div class="alert alert-danger text-center"><i class="fa fa-exclamation-triangle"></i> <?php echo $this->session->flashdata('error'); ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Username', 'username', $data);?>
<div class="col-sm-10">
<?php
$data_username = array(
'id' => 'username',
'name' => 'username',
'class' => 'form-control',
'placeholder' => 'Username',
'value' => $username
)
;?>
<?php echo form_input($data_username);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Password', 'password', $data);?>
<div class="col-sm-10">
<?php
$data_password = array(
'id' => 'password',
'name' => 'password',
'class' => 'form-control',
'placeholder' => 'Password',
'value' => $password
)
;?>
<?php echo form_password($data_password);?>
</div>
</div>
<div class="text-right">
<button type="submit" class="btn btn-primary">Login Now</button>
</div>
</div>
<div class="panel-footer text-right">
Not registered on admin ?
click here
<?php echo form_close();?>
</div>
</div>
</div>
</div>
<?php echo Modules::run('admin/common/footer/index');?>
Try defining a class properties in your Admin_Controller, like this:
<?php
class Admin_Controller extends MX_Controller {
// Change here
protected $ignore;
protected $data;
public function __construct() {
parent::__construct();
$this->load->library('user');
Modules::run('admin/error/permission/check');
$this->ignore = array(
'login',
'logout'
);
if (!in_array($this->router->fetch_class(), $ignore)) {
if ($this->session->userdata('user_id') == FALSE) {
$this->data['error_warning'] = 'You have tried to directly access controller without logging on! Please login.';
redirect('admin');
}
}
}
}
Also, as above, change each reference to either $data or $ignore to be $this->data or $this->ignore. You'll also need to do this inside your Login class too. Always, when referencing class properties (that is, variables declared outside a method within a class), you do so by $this->PROPERTY_NAME.

Form Validation Showing 2 Callback Messages

I am trying to set my own callback message for codeigniter. But when I try to make my own it will show both messages codeigniter one and my custom one. Here is a link to my errors are showing up the top error is one that I would like to use http://postimg.org/image/v9ilejmnt/
How am I able to make it only show my custom error message?
I also use hmvc so I have had to add run($this) in the form validation to make callback work.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('user');
$this->load->library('form_validation');
$this->lang->load('common/login', 'english');
if($this->session->userdata('user_id')) {
redirect('dashboard');
} else {
return false;
redirect('login');
}
}
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|xss_clean|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
redirect('dashboard');
}
}
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->error['warning'] = $this->lang->line('error_login');
return !$this->error;
}
}
}
View
<?php echo modules::run('common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-4 col-sm-4">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title">Administration Login</h2></div>
<div class="panel-body">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<form action="<?php echo $action;?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" size="50" />
</div>
<?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="password" value="" placeholder="Password" class="form-control"/>
</div>
<?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
</div>
</div>
</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>
you forgot to set error message for your your callback.
$this->form_validation->set_message('validate', 'invalid username or password');
return FALSE;
or you can do
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
in your view file you are showing errors for each fields (CI form validation messages) as well as your own <?php echo $error_warning; ?>
you should use following code
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
}
and remove this block from view
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
}
and remove this too
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
I worked it out today. When want to use both validation methods custom and form validation can not use callbacks.
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
if($this->validate()) {
redirect('dashboard');
} else {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
}
}
}

Resources