Welcome Guest, Not a member yet? Register   Sign In
One function form handling
#7

[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



Messages In This Thread
One function form handling - by El Forum - 01-31-2011, 04:20 AM
One function form handling - by El Forum - 01-31-2011, 08:16 AM
One function form handling - by El Forum - 01-31-2011, 08:41 AM
One function form handling - by El Forum - 02-01-2011, 06:30 AM
One function form handling - by El Forum - 02-01-2011, 01:22 PM
One function form handling - by El Forum - 02-02-2011, 02:19 AM
One function form handling - by El Forum - 02-04-2012, 11:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB