Display error message from Admin_Controller to any view - codeigniter

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.

Related

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

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>

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

MethodNotAllowed Exception on form submit

routes.php
use App\Http\Controllers\Task;
use Illuminate\Http\Request;
Route::get('/', function () {
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('tasks', [
'tasks' => $tasks
]);
});
Route::get('Login', 'Login#index');
View: loginform.blade.php
<form method="post" action="http://localhost/blog/public/Login">
<!-- Task Name -->
<div class="form-group">
<div class="col-sm-6">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
{!! Form::label('usernamelabel','Username', ['class'=>'col-sm-3 control-label']) !!}
{!! Form::text('username', '', ['class'=>'form-control','id'=>'username']) !!}
</div>
<div class="col-sm-6">
{!! Form::label('passwordlabel', 'Password', ['class'=>'form-control control-label']) !!}
{!! Form::text('password', '', ['class'=>'form-control','id'=>'password']) !!}
</div>
</div>
<!-- Add Task Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i> Login
</button>
</div>
</div>
{!! Form::close() !!}
Controller: Login.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Models\userloginModel;
use Illuminate\Http\Request;
class Login extends Controller{
protected $request;
public function index(Request $request)
{
echo view('login.loginform');
$foo = new userloginModel();
echo $foo->username = $request->username;
echo $foo->password = $request->password;
}
}
I have try all solutions from Stackoverflow and laracast but i failed to solve this please some one help me from this i am new with laravel..
Your error is in method, u trying to make a post request and your route are receiving a get request, try this:
Route
<?php
//...
Route::get('Login', 'Login#index');
Route::post('Login', 'Login#login');
?>
Controller
<?php
//...
public function index()
{
return view('login.loginform');
}
public function login(Request $request)
{
$foo = new userloginModel();
echo $foo->username = $request->username;
echo $foo->password = $request->password;
}
?>

form validation with array not working

On my form I have some check boxes. And they are in an array. When I submit my form for some reason it clears the Modify check boxes. And then when go back to look not checked.
I have know what's causing issue is $this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit')
It does not seem to like, permission[modify] or permission[modify][] on the set_rules
How am I able to solve this?
Controller Edit Function:
public function edit() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'User Group Name', 'required');
$this->form_validation->set_rules('user_group_id', 'User Group Id', 'required');
$this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit');
if ($this->form_validation->run($this) == FALSE) {
$this->getForm();
} else {
$this->load->model('admin/user/model_user_group');
$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());
$this->db->select('permission');
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->where('user_group_id', $this->session->userdata('user_group_id'));
$user_group_query = $this->db->get();
$permissions = unserialize($user_group_query->row('permission'));
$this->session->set_userdata($permissions);
$this->session->set_flashdata('success', 'Congratulations you have successfully modified' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
redirect('admin/users_group');
}
}
public function modify_check_edit() {
if (!in_array('users_group', $this->session->userdata('modify'))) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to edit' );
}
}
View Form:
<?php echo validation_errors('<div class="alert alert-warning text-center"><i class="fa fa-exclamation-triangle"></i>
', '</div>'); ?>
<?php if ($this->uri->segment(4) == FALSE) { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?>
<?php echo form_open('admin/users_group/add', $data);?>
<?php } else { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'users_group');?>
<?php echo form_open('admin/users_group/edit' .'/'. $this->uri->segment(4), $data);?>
<?php } ?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php
$data_user_name = array(
'id' => 'name',
'name' => 'name',
'class' => 'form-control',
'value' => $name
)
;?>
<?php echo form_input($data_user_name);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Id', 'user_group_id', $data);?>
<div class="col-sm-10">
<?php
$data_user_group_id = array(
'id' => 'user_group_id',
'name' => 'user_group_id',
'class' => 'form-control',
'value' => $user_group_id
)
;?>
<?php echo form_input($data_user_group_id);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Access Permission', 'permission_access', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<?php
$data_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data_checked);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data_not_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data_not_checked);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Modify Permission', 'permission_modify', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $modify)) { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<?php echo form_close();?>
After a good half hour trying to figure it out it was mostly part of the call back not to working with th set_rules() for permission[modify]
Just fixed code here for call back and seems to be working now.
public function modify_check_edit() {
$this->load->library('form_validation');
if (is_array($this->session->userdata('modify'))) {
$permission = !in_array('users_group', $this->session->userdata('modify'));
if ($permission) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
} else {
return TRUE;
}
return TRUE;
} else {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
}
}

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