[eluser]Jmz[/eluser]
I've got a feeling I'm missing something really obvious here but I'll ask anyway.
I've made a controller that lets people edit staff members in the system.
Code: function edit_process(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('second_name', 'Second Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('staff_id', 'Staff ID ', 'trim|required|integer');
if($this->form_validation->run() == FALSE){
//Show form & error
}else{
//Update staff
redirect('admin/staff/');
}
}
If it all goes through successfully the user is redirected to admin/staff. How can I tell this page to display some sort of notification to say the user was updated?
[eluser]Jmz[/eluser]
Ah, I only read that the other day. I knew it was a stupid question!
[eluser]Jmz[/eluser]
I have another question on the code above so I thought I'd ask here rather than create a new thread.
My URL for editing a user looks like mysite.com/admin/staff/edit/1 where the 1 is the ID of the user. If the form validation fails I want to show the form again with the validation error but I can't load the edit view again because I need the ID in the URL and if I redirect it doesn't show the error, would I use flashdata for this aswell or is there a better way?
[eluser]flaky[/eluser]
you could do this
Code: //in the view
<?php echo form_open($url); ?>
.
.
.
//view code
//in the controller
public function edit($id){
$data['url'] = 'staff/edit/' . $id;
.
.
.
$this->load->view('view_file', $data);
[eluser]danmontgomery[/eluser]
Or just have your form submit to the current page, and perform all of the editing on that page so that when validation fails you're still on /admin/staff/edit/1
[eluser]Jmz[/eluser]
flaky. I'm not sure what you mean :red:
noctrum, do you mean do an if to see if any form info has been sent in the controller, if it has process the form, if it hasn't show the form?
[eluser]flaky[/eluser]
Hope this makes it clear what I'm trying to say, by using one single method I can do 2 things, insert and update.
Example
Code: <?php
//Controller
class User extends Controller{
public function __construct(){
parent::__construct();
//load helpers
$this->load->helper('form');
//load models
$this->load->model('user_model');
}
public function index(){
//code that renders some view
}
public function save($user_id=null){
if($user_id != null && (int)$user_id){
if($this->input->post('user_ok')){
$data['user_id'] = $user_id;
$data['user_name'] = $this->input->post('user_name');
redirect('to some url');
}
$data['url'] = "user/save/" . $user_id;
$data['button_name'] = "Update";
$user = $this->user_model->get(array('user_id' => $user_id));
$user = $user[0];
$data['user_name'] = $user['user_name'];
$this->load->view('user_view', $data);
}
else{
if($this->input->post('user_ok')){
$user['user_name'] = $this->input->post('user_name');
$this->user_model->save($data);
redirect('to some url');
}
$data['user_name'] = "";
$data['url'] = "user/save";
$data['button_name'] = "Add";
$this->load->view('user_view', $data);
}
}
}
//Model
class User_model extends Model{
public function __construct(){
parent::__construct();
}
public function save($data){
if(isset($data['user_id'])){
$this->db->where('user_id', $data['user_id']);
$this->db->update('user', $data);
}
else{
$this->db->insert('user', $data);
}
}
public function get($data){
//it is best if I would select the fields I need, since it's only an example I won't go to that extent
if(isset($data['user_id']))
$this->db->where('user_id', $data['user_id']);
return $this->db->get('user')->result_array();
}
}
?>
//View
<div>
<?php echo form_open($url); ?>
<?php echo form_label('User'); ?>
<?php echo form_input('user_name', $user_name); ?>
<br/>
<?php echo form_submit('user_ok', $button_name); ?>
<?php echo form_close();
</div>
[eluser]danmontgomery[/eluser]
[quote author="Jmz" date="1263590711"]flaky. I'm not sure what you mean :red:
noctrum, do you mean do an if to see if any form info has been sent in the controller, if it has process the form, if it hasn't show the form?[/quote]
Code: if($this->form_validation->run())
{
// Process form
// Redirect if needed, load a different view, etc
}
else
{
// Show validation errors
// Show form
}
[eluser]flaky[/eluser]
Code: $this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('second_name', 'Second Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('staff_id', 'Staff ID ', 'trim|required|integer');
if($this->form_validation->run()){
$data['first_name'] = $this->input->post('first_name');
$data['second_name'] = $this->input->post('second_name');
$data['email'] = $this->input->post('email');
$data['staff_id'] = $this->input->post('staff_id');
$this->some_model->save($data);
redirect('to_some_url_you_want');
}
else{
$this->load->view('some_view');
}
|