[eluser]Unknown[/eluser]
Hi
can anyone help with a issue which I believe is a simple Newbie question....
I want to have 1 view file set up for both add and edit options on a form. How do I combine the way CI holds the validation values for a new form compared to when you want it to return the values from a database for editting?
IE
Setting up the controller for a "New" form subbmission
Code:
function index()
{
$pageData = array();
$rules = $this->setRules();
$this->validation->set_rules($rules);
$fields = $this->setFields();
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->validation->run() == FALSE) {
$pageData['formAttributes'] = array('id' => 'contact-form');
$pageData['pageTitle'] = 'New Booking Repair Request';
$pageData['booking'] = "";
$this->load->view('Repair/booking_form', $pageData);
} else {
//$this->sendEmail();
$this->load->model('repair_model');
// save to database
if($this->repair_model->saveBooking()){
redirect('/repair/confirmation', 'refresh');
}else{
$this->load->view('Repair/booking_form', $pageData);
}
}
}
This is a snippet from the setFields method
Code:
private function setFields()
{
$fields['firstName']= '';
}
This is the code for setting up the "EDIT" view
Code:
public function edit()
{
$pageData['formAttributes'] = array('id' => 'contact-form');
$pageData['pageTitle'] = 'Edit Booking Repair Request';
$repairId = $this->uri->segment(3);
if (!isset($repairId)) {
redirect('/repair/','refresh');
} else {
$this->load->model('repair_model');
$pageData['booking'] = $this->repair_model->loadBooking($repairId);
$rules = $this->setRules();
$this->validation->set_rules($rules);
//$fields = $this->setFields();
$this->validation->set_fields($fields);
if ($this->validation->run() == FALSE) {
$this->load->view('Repair/booking_form', $pageData);
} else {
$fields = $this->setFields();
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<p class="hint error"><span>', '</span></p>');
$this->repair_model->saveBooking($repairId);
$this->load->view('Repair/booking_form', $pageData);
}
}
}
This is a snippet from my view
Code:
<div class="form-field">
<? $firstName = @field($this->validation->firstName, $booking->firstName);?>
<input type="text" value="<?= $firstName ?>" id="firstName" maxlength="" name="firstName" />
</div>
At the moment.
This is the code from my helper
Code:
<?php
function field($validation, $database = NULL, $last = ''){
$value = (isset($validation)) ? $validation : ( (isset($database)) ? $database : $last);
return $value;
}
So the problem is that the helper is always trying to show the value of the validation variable instead of the object from the $booking object.
Am I doing something completing wrong and barking up the wrong tree. Is there a simplier way to reuse the same view for add and edit actions?
thanks
simon