Welcome Guest, Not a member yet? Register   Sign In
Add / Edit control with 1 view file
#1

[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">
                &lt;? $firstName = @field($this->validation->firstName, $booking->firstName);?&gt;
                &lt;input type="text" value="&lt;?= $firstName ?&gt;" id="firstName" maxlength="" name="firstName"  /&gt;
            </div>

At the moment.

This is the code from my helper

Code:
&lt;?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


Messages In This Thread
Add / Edit control with 1 view file - by El Forum - 05-11-2009, 07:35 AM
Add / Edit control with 1 view file - by El Forum - 05-11-2009, 08:19 AM
Add / Edit control with 1 view file - by El Forum - 05-11-2009, 08:32 AM
Add / Edit control with 1 view file - by El Forum - 05-11-2009, 09:04 AM
Add / Edit control with 1 view file - by El Forum - 05-11-2009, 10:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB