-
blackbulldog
Junior Member
-
Posts: 35
Threads: 9
Joined: Feb 2015
Reputation:
0
Hi,
Got a simple 'edit' style form which, when submitted, gets validated. If there are errors then I want to redisplay the form with whatever the user entered ... can;t for the life of me recal how to do the redisplay!!!
Here's the controller which loads the form:
Code: public function edit_staff($par_id)
{
$this->data['staff'] = $this->M_staff->get_selected($par_id);
$alldepts=$this->M_department->get_all();
$this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts);
$this->data['title'] = 'staff Management';
$this->data['message'] = $this->session->flashdata('message');
$this->data['main'] = 'v_staff_edit';
$this->load->view('v_template', $this->data, 'refresh');
}
Here's the controller that validates it and 'should' do the redisplay
Code: public function edit_staff_submit()
{
if ($this->form_validation->run('staff') == true)
{
$data = array(
'fname'=> $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'dept'=> $this->input->post('dept')
);
$this->M_staff->update_record($this->input->post('staffid'),$data);
$this->session->set_flashdata('message', "<p>staff updated successfully.</p>");
redirect('staff/index', 'refresh');
}
else // If validation fails, r.edisplay
{
What to do here, how do i redisplay view?????
}
}
Here's the view:
Code: <div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php
echo form_open("staff/edit_staff_submit/");
echo form_hidden('staffid', $staff->staffid);
?>
<fieldset>
<div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="fname" class="control-label">First Name</label>
</div>
<div class="col-md-8">
<input id="fname" name="fname" placeholder="fname" type="text" class="form-control" value="<?php echo set_value('fname', $staff->fname); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="fname" class="control-label">Last Name</label>
</div>
<div class="col-md-8">
<input id="lname" name="lname" placeholder="Last name" type="text" class="form-control" value="<?php echo set_value('lname', $staff->lname); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="Department" class="control-label">Department</label>
</div>
<div class="col-md-8">
<?php echo form_dropdown('dept', $dropdown, $staff->dept);?>
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-md-8 text-left">
<input id="btn_update" name="btn_update" type="submit" class="btn btn-primary" value="Update" />
<?php echo anchor('staff/index','<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel">')?>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
-
paralogizing
paralogizing
-
Posts: 12
Threads: 0
Joined: Feb 2015
Reputation:
0
-
blackbulldog
Junior Member
-
Posts: 35
Threads: 9
Joined: Feb 2015
Reputation:
0
(02-17-2015, 01:06 PM)Avenirer Wrote: As I see it, you have no validation rules set: you do a form_validation->run() but without the validating rules. To redisplay the view, you should take the staff id from the hidden form element, and with it, you should do the same as you've done in the edit_staff method.
The form validation is loaded from the config file.
Surely just copying the code from the other controller isn't the way to go, is it? And a simple redirect to the edit form controller will simply overwrite what the user entered, won't it?
-
Avenirer
Senior Member
-
Posts: 419
Threads: 15
Joined: Oct 2014
Reputation:
21
02-17-2015, 01:40 PM
(This post was last modified: 02-17-2015, 01:40 PM by Avenirer.)
Well... I would have done everything in one method...
PHP Code: public function edit_staff($par_id) {
$par_id = ($this->input->post('staffid')) ? (int) $this->input->post('staffid') : (int) $par_id;
if($this->form_validation->run('staff') === FALSE) { $this->data['staff'] = $this->M_staff->get_selected($par_id); $alldepts=$this->M_department->get_all(); $this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts); $this->data['title'] = 'staff Management'; $this->data['message'] = $this->session->flashdata('message',"<p>There were errors in your form</p>"); $this->data['main'] = 'v_staff_edit'; $this->load->view('v_template', $this->data);
}
else { $data = array( 'fname'=> $this->input->post('fname'), 'lname' => $this->input->post('lname'), 'dept'=> $this->input->post('dept') ); $this->M_staff->update_record($this->input->post('staffid'),$data); $this->session->set_flashdata('message', "<p>staff updated successfully.</p>"); redirect('staff/index', 'refresh'); } }
And the form would just send to the same page:
PHP Code: <div class="row"> <div class="col-md-6 col-md-offset-3 well"> <?php echo form_open(); // it will send the form data to same page echo form_hidden('staffid', $staff->staffid); ?> ....and all the form elements that come after these...
I hope it helped you.
-
blackbulldog
Junior Member
-
Posts: 35
Threads: 9
Joined: Feb 2015
Reputation:
0
(02-17-2015, 01:40 PM)Avenirer Wrote: Well... I would have done everything in one method...
PHP Code: public function edit_staff($par_id) {
$par_id = ($this->input->post('staffid')) ? (int) $this->input->post('staffid') : (int) $par_id;
if($this->form_validation->run('staff') === FALSE) { $this->data['staff'] = $this->M_staff->get_selected($par_id); $alldepts=$this->M_department->get_all(); $this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts); $this->data['title'] = 'staff Management'; $this->data['message'] = $this->session->flashdata('message',"<p>There were errors in your form</p>"); $this->data['main'] = 'v_staff_edit'; $this->load->view('v_template', $this->data);
}
else { $data = array( 'fname'=> $this->input->post('fname'), 'lname' => $this->input->post('lname'), 'dept'=> $this->input->post('dept') ); $this->M_staff->update_record($this->input->post('staffid'),$data); $this->session->set_flashdata('message', "<p>staff updated successfully.</p>"); redirect('staff/index', 'refresh'); } }
And the form would just send to the same page:
PHP Code: <div class="row"> <div class="col-md-6 col-md-offset-3 well"> <?php echo form_open(); // it will send the form data to same page echo form_hidden('staffid', $staff->staffid); ?> ....and all the form elements that come after these...
I hope it helped you.
Avenirer,
Thanks mate, that's exactly what I was looking for .
|