[eluser]seanloving[/eluser]
Here are the basics of how I do it - these are methods in my products controller
application/modules/inventory/controllers/products
Code:
function add()
{
// MODEL
$headerdata['title'] = "Add Product";
$maindata = $this->_form(); // get the blank form as a view partial
// VIEW
$this->load->view('header', $headerdata);
$this->load->view('inventory/products', $maindata);
$this->load->view('footer');
}
function edit($pn)
{
// MODEL
$headerdata['title'] = "Edit Product";
if( ! isset($_POST) ) $_POST = $this->products_model->get_row( array('pn'=>$pn) );
$maindata = $this->_form( $_POST ); // get the pre-populated form as a view partial
// VIEW
$this->load->view('header', $headerdata);
$this->load->view('inventory/products', $maindata);
$this->load->view('footer');
}
// returns the form as a view partial (either blank, or else pre-populated with data)
function _form( $post )
{
// form request (or sumbission) from either add() or edit($pn)
$action = isset($post) ? 'edit' : 'add';
// submits back to 'add' or 'edit'
$this->form->open('inventory/products/'.$action)
->text('product|productID', 'Product', 'required|trim|max_length[10]', $this->input->post('product'), array('style'=>''))
->text('description|descriptionID', 'Description', 'required|trim|max_length[80]', $this->input->post('description'), array('style'=>'') )
->submit( ($action=='add') ? 'Add': 'Update', $action )
->model('products_model', $action.'_product') //interacts with database if submitted form is valid
->validate(); // not sure if FGL requires calling this validate() method
if( $this->form->valid )
{
redirect( 'somewhere' ); // set some flash data to give a nice success message
}
$data['form'] = $this->form->get(); // gets the submitted form data as a string
$data['errors'] = $this->form->errors;
return $data;
}
cheers