So I have this form in which a user can update his information:
The problem comes when some of these inputs are optional. Let's say a user updates his email and password, then the update updates but when a user only edits his email and leave the password inputs blank the password in the database should not be updated....it currently changes the password as well even when they're empty.
Here is my HTML:
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<?php echo form_open('users/edit/'.$item->id); ?>
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Basics</li>
<li class="">About Me</li>
</ul>
<br>
<div class="tab-content">
<!-- Basics -->
<div class="tab-pane active" id="basics">
<!-- Email -->
<div class="form-group">
<?php echo form_label('Email', 'email'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-envelope" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'email',
'id' => 'email',
'maxlength' => '150',
'class' => 'form-control',
'value' => $item->email,
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Avatar Image -->
<div class="form-group">
<?php echo form_label('Avatar Image URL', 'avatar_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></i></div>
<?php
$data = array(
'name' => 'avatar_img',
'id' => 'avatar_img',
'class' => 'form-control',
'placeholder' => '96x96 Pixels',
'value' => $item->avatar_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Cover Image -->
<div class="form-group">
<?php echo form_label('Cover Img URL', 'cover_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'cover_img',
'id' => 'cover_img',
'class' => 'form-control',
'value' => $item->cover_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Occupation -->
<div class="form-group">
<?php echo form_label('Occupation', 'occupation'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-briefcase" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'occupation',
'id' => 'occupation',
'class' => 'form-control',
'value' => $item->occupation
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Website -->
<div class="form-group">
<?php echo form_label('Website', 'website'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-link" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'website',
'id' => 'website',
'class' => 'form-control',
'value' => $item->website
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Password -->
<div class="form-group">
<?php echo form_label('Password', 'password'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password',
'id' => 'password',
'class' => 'form-control',
'value' => set_value('password'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
<!-- Password2 -->
<div class="form-group">
<?php echo form_label('Confirm Password', 'password2'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password2',
'id' => 'password2',
'class' => 'form-control',
'value' => set_value('password2'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
</div>
</div>
</div>
<?php echo form_submit('mysubmit', 'Update User', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
and here is what I have tried:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password'] = ($this->input->post('password') && !empty($this->input->post('password'))) ? $this->input->post('password') : NULL;
}
if($this->input->post('password2') != ''){
$data['password2'] = ($this->input->post('password2') && !empty($this->input->post('password2'))) ? $this->input->post('password2') : NULL;
}
// Update User
$this->User_model->update($id, $data);
?>
but it just don't work, so I tried making more simple:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
if($this->input->post('password2') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
Update method in Userr model:
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
Thanks in advance.
If you want the password(s) to be updated on database only if the password field(s) are NOT empty, do it like below:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website')
);
if(trim($this->input->post('password')) != ''){
$data['password'] = password_hash(trim($this->input->post('password')), PASSWORD_DEFAULT);
}
if(trim($this->input->post('password2')) != ''){
$data['password2'] = password_hash(trim($this->input->post('password2')), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
I removed "password" and "password2" from first array list and kept in condition check. I added trim in case the fields have white-spaces in it.
I hope this will work!
edit this code.
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
to
if(!empty($this->input->post('email'))){
$data['email'] = $this->input->post('email');
}
if(!empty($this->input->post('avatar_img'))){
$data['avatar_img'] = $this->input->post('avatar_img');
}
if(!empty($this->input->post('cover_img'))){
$data['cover_img'] = $this->input->post('cover_img');
}
if(!empty($this->input->post('occupation'))){
$data['occupation'] => $this->input->post('occupation');
}
if(!empty($this->input->post('website'))){
$data['website'] = $this->input->post('website');
}
if(!empty($this->input->post('password'))){
$data['password'] = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
}
if(!empty($this->input->post('password2'))){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
Related
Anyone pls help ...
This is my Controller code (Actions.php)
public function add_admin()
{
$data['employee'] = $this->employee_model->get_new();
$data['department'] = $this->employee_model->dpdown_admins();
$data['gender'] = $this->employee_model->dpdown_gender();
$data['status'] = $this->employee_model->dpdown_status();
$data['attainment'] = $this->employee_model->dpdown_attainment();
$rules = $this->employee_model->rules['insert'];
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$post = $this->employee_model->array_from_post(array(
'ID_number',
'employee_no',
'department_id',
'department_position',
'tin_number',
'sss_number',
'ph_number',
'first_name',
'middle_name',
'last_name',
'suffix_name',
'street',
'barangay',
'city',
'zipcode',
'birthday',
'age',
'gender_id',
'nationality',
'religion',
'status_id',
'telephone_number',
'cellphone_number',
'email',
'educational_att_id',
'diploma_title',
'school_name',
'year_graduated',
'company1_name',
'position1_title',
'inclusive_dates1',
'company2_name',
'position2_title',
'inclusive_dates2',
'company3_name',
'position3_title',
'inclusive_dates3',
'date_employed'
));
$data = $this->employee_model->save($post);
return self::redirect_to('human_resource/admin', '<b>Success</b><br>New Admin Employee has been successfully Registered.');
}
$data['error'] = validation_errors('<li class="error">', '</li>');
$data['content'] = 'human_resource/actions/add_admin';
return $this->load->view('templates', $data);
}
and then this is how my model looks like (employee_model.php)
these are for my rules
public $rules = array('insert' => array(
'department_position' => array(
'field' => 'department_position',
'label' => 'Employee Position',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'ID_number' => array(
'field' => 'ID_number',
'label' => 'ID Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'employee_no' => array(
'field' => 'employee_no',
'label' => 'Employee Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'tin_number' => array(
'field' => 'tin_number',
'label' => 'Tin Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'sss_number' => array(
'field' => 'sss_number',
'label' => 'SSS Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'ph_number' => array(
'field' => 'ph_number',
'label' => 'Phil Health Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),));
then this is my save function still inside the same model
public function save($post, $employee_id = NULL)
{
if($this->timestamp == TRUE)
{
$now = date('Y-m-d H:i:s');
$employee_id || $post['created_at'] = $now;
$post['updated_at'] = $now;
}
if($employee_id === NULL)
{
!isset($post[$this->primary_key]) || $post[$this->primary_key] = NULL;
$this->db->insert($this->table_name, $post);
$employee_id = $this->db->insert_id();
}else{
$filter = $this->filter;
$this->db->where($this->primary_key, $filter($employee_id))
->update($this->table_name, $post);
}
}
then this is my view form
i did not put all in here just the 1/4 of my view code
<div class="main-content">
<div class="main-content-inner">
<div class="breadcrumbs ace-save-state" id="breadcrumbs">
<ul class="breadcrumb">
<li>
<i class="ace-icon fa fa-tachometer home-icon"></i>
Dashboard
</li>
<li>
<i class="ace-icon fa fa-user home-icon"></i>
Administration Table
</li>
<li>
New Admin Form
</li>
<li class="active">Application Form</li>
</ul>
</div>
<!-- /end of breadcrumb -->
<div class="page-content">
<div class="page-header">
<h1>
<i class="fa fa-user-plus"></i>
New Admin Form
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Application Form
</small>
</h1>
</div>
<!-- end of page header -->
<?php echo form_open(NULL, array('class' => 'form-horizontal', 'role' => 'form'));?>
<div class="row">
<div class="col-xs-12">
<!-- table content -->
<div class="widget-box">
<div class="widget-header widget-header-blue widget-header-flat">
<h4 class="widget-title lighter">Form Wizard</h4>
</div>
<!-- end of widget header -->
<!-- R-1 -->
<div class="widget-body">
<div class="widget-main" style="padding-bottom: 0;">
<h4 class="lighter block green">Employee General Information
<span class="help-button" style="background-color: red;" data-rel="popover" data-trigger="hover" data-placement="right" data-content="If a field is not applicable please type N/A" title="Notice!">
<i class="fa fa-exclamation"></i>
</span>
</h4>
<div class="row">
<!-- B-1 col -->
<div class="col-xs-12 col-sm-4">
<div class="widget-box">
<div class="widget-header">
<h5 class="widget-title">General Information</h5>
</div>
<!-- end of widget header -->
<div class="widget-body">
<div class="widget-main">
<!-- employee information B-1-->
<!-- first name -->
<?php echo form_label('First Name', 'first_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('first_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('first_name', set_value('first_name', $employee->first_name),
array( 'type' => 'text',
'name' => 'first_name',
'id' => 'firstname',
'class' => 'form-control')); ?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('first_name'); ?>
</div>
</div>
<?php endif ?>
<!-- last name -->
<?php echo form_label('Last Name', 'last_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('last_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('last_name', set_value('last_name', $employee->last_name),
array( 'type' => 'text',
'name' => 'last_name',
'id' => 'lastname',
'class' => 'form-control'));?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('last_name'); ?>
</div>
</div>
<?php endif ?>
<!-- middle name -->
<?php echo form_label('Middle Name', 'middle_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('middle_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('middle_name', set_value('middle_name', $employee->middle_name),
array( 'type' => 'text',
'name' => 'middle_name',
'id' => 'middlename',
'class' => 'form-control'));?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('middle_name'); ?>
</div>
</div>
<?php endif ?>
<!-- suffix name -->
<?php echo form_label('Suffix', 'suffix_name', array('class' => 'control-label'));?>
<div class="form-group" style="margin-bottom: 0;">
<div class="col-sm-6">
<?php echo form_input('suffix_name', set_value('suffix_name', $employee->suffix_name),
array( 'type' => 'text',
'name' => 'suffix_name',
'id' => 'suffixname',
'class' => 'form-control'));?>
</div>
</div>
<hr>
</div>
</div>
</div>
</div>
<!-- end of B-1 col -->
please anyone i really need your help
You need to use form_open_multipart
This function is absolutely identical to form_open() ... , except that
it adds a multipart attribute, which is necessary if you would like to
use the form to upload files with.
Ref: https://www.codeigniter.com/user_guide/helpers/form_helper.html
I got stuck in a a problem in which I need to update some info from database in the same page that is shown.
On this case I'm fetching some "global settings" from a website in an index page which comes with a form in it. Here is a picture of it just to make it more clear to understand what I mean.
As you can see I created the button and I made it possible to see the values from the database, the problem is that I can not figure it out how to update it from there. Can somebody suggest an idea?
Here is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings extends Admin_Controller {
public function index()
{
$this->form_validation->set_rules('website_favicon', 'Favicon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_logo', 'Logon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_name', 'Website Name', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits', 'Credits', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits_link', 'Credits Link', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_copyright', 'Copyright', 'trim|required|min_length[4]');
if($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->Settings_model->get_website_data();
//Load View Into Template
$this->template->load('admin', 'default', 'settings/index', $data);
} else {
// Create website settings
$data = array(
'website_favicon' => $this->input->post('website_favicon'),
'website_logo' => $this->input->post('website_logo'),
'website_name' => $this->input->post('webiste_name'),
'website_credits' => $this->input->post('website_credits'),
'website_credits_link' => $this->input->post('website_credits_link'),
'website_copyright' => $this->input->post('website_copyright'),
);
// Update User
$this->Settings_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'website settings',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'User (' . $data["username"] . ') updated the website settings'
);
// Add Activity
$this->Activity_model->add($data);
//Create Message
$this->session->set_flashdata('success', 'Website setting has been updated');
//Redirect to Users
redirect('admin/settings');
}
}
}
Here is my model:
<?php
class Settings_model extends CI_MODEL
{
function __construct()
{
parent::__construct();
$this->table = 'website_settings';
}
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function get_website_data()
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('id', 1);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
}
and here is my view(index.php) with the form:
<h2 class="page-header">Website Settings</h2>
<?php if($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alert!</h4>
<?php echo $this->session->flashdata('success') ?></div>
<?php endif; ?>
<?php if($this->session->flashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Alert!</h4>
<?php echo $this->session->flashdata('error') ?></div>
<?php endif; ?>
<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('admin/settings/index/'.$item->id); ?>
<!-- Website Favicon -->
<div class="form-group">
<?php echo form_label('Website Favicon', 'title'); ?>
<?php
$data = array(
'name' => 'website_favicon',
'id' => 'website_favicon',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_favicon
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Logo -->
<div class="form-group">
<?php echo form_label('Website Logo', 'website_logo'); ?>
<?php
$data = array(
'name' => 'website_logo',
'id' => 'website_logo',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_logo
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Name -->
<div class="form-group">
<?php echo form_label('Website Name', 'website_name'); ?>
<?php
$data = array(
'name' => 'website_name',
'id' => 'website_name',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_name
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits -->
<div class="form-group">
<?php echo form_label('Website Credits to', 'website_credits'); ?>
<?php
$data = array(
'name' => 'website_credits',
'id' => 'website_credits',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits Link -->
<div class="form-group">
<?php echo form_label('Website Credits to Link', 'website_credits_link'); ?>
<?php
$data = array(
'name' => 'website_credits_link',
'id' => 'website_credits_link',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits_link
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Copyright -->
<div class="form-group">
<?php echo form_label('Copyrights', 'website_copyright'); ?>
<?php
$data = array(
'name' => 'website_copyright',
'id' => 'website_copyright',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_copyright
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website First Ad -->
<div class="form-group">
<?php echo form_label('Ad One', 'website_first_ad'); ?>
<?php
$data = array(
'name' => 'website_first_ad',
'id' => 'website_first_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_first_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Second Ad -->
<div class="form-group">
<?php echo form_label('Ad Two', 'website_second_ad'); ?>
<?php
$data = array(
'name' => 'website_second_ad',
'id' => 'website_second_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_second_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Third Ad -->
<div class="form-group">
<?php echo form_label('Ad Three', 'website_third_ad'); ?>
<?php
$data = array(
'name' => 'website_third_ad',
'id' => 'website_third_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_third_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<?php echo form_submit('mysubmit', 'Update Website', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
Thanks for helping.
Check if the data is posted thru the controller. then use set_value to your input fields to retain the values after submit
CONTROLLER
public function index(){
if($this->input->post()){
//set rules for form validation
if($this->form_validation->run() !== FALSE){
//then update
}
}
//your views, data or any other things you do
}
VIEW
echo form_input('name', set_value('name'));
On click of the button you can bind the ajax call to submit the data to the update action of the controller and you can handle response to show relevant message on the same page.
Sample ajax call
$.ajax({
url:'settings/update',//controller action
type:'POST',
dataType:'JSON',
data:{'data':data,'id':id},//form data you need to upate with the id
success:function(response) {
//show success message here
},
error:function(response) {
//show error message here
}
});
Hope this helps.
I want to create the custompasswordhasher for my cakephp project. the cakephp version is 2.5. I have follow the cakephp cook book and create the following custom class in directory Controller/Auth/CustomPasswordHasher.php
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
$hasher = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
return $hasher;
}
public function check($password, $hashedPassword) {
//debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
$password = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
echo $password."==".$hashedPassword;exit;
return password_verify($password, $hashedPassword);
}
}
and here is my login function in the controller
public function admin_login() {
if ($this->Auth->loggedIn()) {
return $this->redirect($this->Auth->redirect());
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Username or password is incorrect', 'error');
}
}
}
and in appController.php I config function
public function beforeFilter() {
if ($this->request->prefix == 'admin') {
$this->layout = 'admin';
AuthComponent::$sessionKey = 'Auth.User';
$this->Auth->loginAction = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'administrators', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->authenticate = array(
'all' => array(
'scope' => array(
'User.is_active' => 1
)
),
'Form' => array(
'userModel' => 'User',
'passwordHasher' => array(
'className' => 'Auth/CustomPasswordHasher'
)
)
);
$this->Auth->allow('login');
} else {
/* do another stuff for user authentication */
}
}
And here is my login form.
<div class="login-box">
<div class="login-logo">Admin Login</div>
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->create(); ?>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.username',
array(
'label' => false,
'class' => 'form-control',
'placeholder' => 'Username',
'autocomplete' => 'off',
'autofocus' => true,
'value' => #$username
)
);
?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.password',
array(
'type' => 'password',
'label' => false,
'class' => 'form-control',
'placeholder' => 'Password',
'value' => #$password
)
);
?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck ">
<label>
<input type="checkbox" name="data[Admin][remember_me]"> Remember Me </label>
</div>
</div>
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div>
</div>
<?php echo $this->Form->end(); ?>
I forgot my password<br>
</div>
</div>
how every when submit form. I got the stack trace
So everyone can you help me with this?
I call wrong className in my controller . it should be "Custom" not "CustompasswordHasher". Cake will automatic add that.
I am new to Ion Auth codeigniter library. Now I am making a custom bootstrap form to add a user using modal but I always get errors. Any help would be much appreciated. The modal is inside the userList.php view that displays all registered users.
Controller: Users.php
function create_user()
{
$this->data['title'] = "Create User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$tables = $this->config->item('tables','ion_auth');
$identity_column = $this->config->item('identity','ion_auth');
$this->data['identity_column'] = $identity_column;
// validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required');
$this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required');
if($identity_column!=='email')
{
$this->form_validation->set_rules('identity',$this->lang->line('create_user_validation_identity_label'),'required|is_unique['.$tables['users'].'.'.$identity_column.']');
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email');
}
else
{
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique[' . $tables['users'] . '.email]');
}
$this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'trim');
$this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'trim');
$this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
if ($this->form_validation->run() == true)
{
$email = strtolower($this->input->post('email'));
$identity = ($identity_column==='email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
);
var_dump($first_name);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($identity, $password, $email, $additional_data))
{
// check to see if we are creating the user
// redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth", 'refresh');
}
else
{
// display the create user form
// set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company'),
);
$this->data['phone'] = array(
'name' => 'phone',
'id' => 'phone',
'type' => 'text',
'value' => $this->form_validation->set_value('phone'),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->_render_page('userList', $this->data);
}
}
View: userList.php
<div class="modal fade" id="add-user" role="dialog">
<div class="modal-dialog modal-default">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 id="modal-user-profile" class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<?php echo form_open('users/create_user', ['class' => 'form-horizontal', 'role' => 'form']); ?>
<div class="form-group">
<label for="first_name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<?php echo form_input($first_name);?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password', 'id' => 'password', 'class' => 'form-control', 'placeholder' => 'Password']); ?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password_confirm', 'id' => 'password_confirm', 'class' => 'form-control', 'value' => set_value('password_confirm'), 'placeholder' => 'Confirm Password']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email'), 'placeholder' => 'Email']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Contact No</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'contact', 'id' => 'contact', 'class' => 'form-control', 'value' => set_value('contact'), 'placeholder' => 'Contact']); ?>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Group</label>
<div class="col-sm-10">
<select class="form-control" id="group">
<option>--</option>
<option>Admin</option>
<option>Client</option>
<option>BIR</option>
</select>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<select class="form-control" id="status">
<option>--</option>
<option>Active</option>
<option>Inactive</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default create-user">Create</button>
<button type="button" class="btn btn-danger create">Cancel</button>
</div>
</div>
<?php echo form_close(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
What errors are you getting??
In your controller, Use this to verify your results
After query;
echo $this->db->last_query();
var_dump($_POST);exit;
First check all the post data and the query and tell the errors you're getting
How can i make a registration form where i upload an image for my profile picture, and before clicking register, there's a preview of the image that i want to set as the profile picture using CI??
here's my registration form code
register_model.php
<?php
class Register_model extends CI_Model
{
function create_user($photo_name)
{
$new_user_insert_data = array(
'id' => '',
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password')),
'name' => $this->input->post('name'),
'major' => $this->input->post('user_major'),
'year' => $this->input->post('user_year'),
'bio' => $this->input->post('bio'),
'profpic' => $photo_name,
'insta' => $this->input->post('instagram'),
'twitter' => $this->input->post('twitter'),
'facebook' => $this->input->post('facebook'),
'vote_count' => '10',
'vote' => '0',
'gender' => $this->input->post('gender'),
);
$insert = $this->db->insert('user', $new_user_insert_data);
return $insert;
}
}
?>
Here's the view
register.php
<div class="margin-top-87">
<div class="container">
<h1>Register</h1>
<hr>
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
<?php
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Image Upload - End
//Thumbnail Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
$config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}
?>
<h6>Please use real photo, not an avatar</h6>
<?php
$data= array
(
'name' => 'userfile',
'type' => 'file',
'class' => 'form-control',
'id' => 'form_register'
);
echo form_upload($data);
?>
</div>
</div>
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-info fade in">
<strong>Info!</strong> Please fill with real information of yours.
</div>
<?php
if(validation_errors('<p class="error">'!=""))
{?>
<div class="alert alert-danger fade in">
×
<strong>Attention!</strong> <?php echo validation_errors('<p class="error">') ?>
</div><?php
}?>
<h3>Personal info</h3>
<?php $attributes = array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_register'); echo form_open('index/create_user',$attributes); ?>
<div class="form-group">
<label class="col-lg-3 control-label">UC Student email :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'email',
'placeholder' => 'Ciputra student email. Example : email#student.ciputra.ac.id',
'type' => 'email',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Name :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'name',
'placeholder' => 'Full name. Example : Front Middle Last',
'type' => 'text',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Gender :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Gender',
'Man' => 'Man',
'Woman' => 'Woman'
);
echo form_dropdown('gender', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Major :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Insert your major',
'CB' => 'CB',
'FDB' => 'FDB',
'IBM-IC' => 'IBM-IC',
'IBM-RC' => 'IBM-RC',
'IBA' => 'IBA',
'IHTB' => 'IHTB',
'IMT' => 'IMT',
'INA' => 'INA',
'MCM' => 'MCM',
'MIS' => 'MIS',
'PSY' => 'PSY',
'VCD' => 'VCD'
);
echo form_dropdown('user_major', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Year :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array('' => 'Year joined UC');
for ($i = date('Y'); $i >= 2006; $i--)
{
$options[$i] = $i;
}
echo form_dropdown('user_year', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'password',
'placeholder' => 'Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'cpassword',
'placeholder' => 'Confirm Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Short Bio :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'bio',
'placeholder' => 'Short bio (max 128 character)',
'type' => 'text',
'class' => 'form-control',
'maxlength' => '128',
'rows' => '3'
);
echo form_textarea($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Facebook :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'facebook',
'placeholder' => 'Facebook Profile link. Example : facebook.com/name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Twitter :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'twitter',
'placeholder' => 'Twitter Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Instagram :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'instagram',
'placeholder' => 'Instagram Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'register',
'value' => 'Register',
'type' => 'submit',
'class' => 'btn btn-primary',
);
echo form_submit($data);
?>
<span></span>
<?php
$data= array
(
'name' => 'reset',
'value' => 'Clear',
'type' => 'reset',
'class' => 'btn btn-default',
);
echo form_reset($data);
?>
</div>
</div>
</form>
<?php echo form_close(); ?>
</div>
</div>
</div>
<hr>
</div>
The "preview before uploading" thing is a javascript trick that loads the local file into the <img> source. The rest is simple file upload where you check the file types, size, resolution etc.
Here is your answer: Preview an image before it is uploaded