Welcome Guest, Not a member yet? Register   Sign In
Multiple forms in one controller validation issues.
#1

[eluser]Unknown[/eluser]
First of all, thanks for making an excellent product. I should preface this by saying that I'm a complete noob to CodeIgniter and php in general. I've been working on this problem for what feels like 6 months and have done endless Google searches. The problem is when I have more than one form in any controller the validation run on every field whether or not it has been submitted (my best guess). I'm using the brilliant Form Generation Library by Frank Michel and the Template library and Ion Auth. So here's the controller, I apologize for my horrible coding.

This is part of a crm system ( i.e. I want to edit a Customer and it's contacts from the same page)
Code:
public function customer() {
  $customer_name = $this->customer_form($this->uri->segment(4));
  $this->contact_form($this->uri->segment(6),$this->uri->segment(4));
  $this->db->order_by('CON.FName','asc');
  $this->db->where('CON.CustomerID',$this->uri->segment(4));
  $results = $this->clist->get_contacts();
  $this->template->write('content', '<fieldset><legend>Contacts for '. $customer_name.'</legend>'.$results['table'].'</fieldset>');
  $this->template->render();
}
And here are the two functions in the same controller, I attempted to separate them so I can pick and choose where to reuse each form.

Code:
function customer_form($CID) {
$cusform = new Form();
if($CID)  {
   $this->db->where('CUS.ID',$CID);
   $customer_loader = $this->clist->get_customers();
   $customer_loader = $customer_loader[0];
   $cusform->fieldset('Update '. $customer_loader['CName']);
   $this->template->write('title', 'CRM - Update Customer');
   $this->message = $customer_loader['CName']. ' has been updated';
   $cusform->hidden('CUSID',$CID);
  } else {
   $cusform->fieldset('New Customer');
   $this->template->write('title', 'CRM - New Customer');
   $this->message = 'Customer has been Created';
  }
  $cusform->text('Name|Customer_Name', 'Customer Name', 'required|trim|max_length[40]',$customer_loader['CName'],'placeholder = Enter Customer Name');
  $cusform->textarea('BackGround|Customer_background','Background','trim',$customer_loader['Background'],'cols=25, rows=4, placeholder = Brief background here:');
  $cusform->text('Website|Customer_website', 'Website', 'trim|max_length[45]',$customer_loader['Website'],'placeholder = e.g. www.matrixtooling.com');
  $cusform->text('NDANotes', 'NDA Notes', 'trim',$customer_loader['NDANotes'],'cols=25, rows=4, placeholder = Description');
  $cusform->upload('file', 'Upload NDA', '', 'allowed_types=pdf|zip|jpg|png|doc|xls|docx|xlsx|ppt|txt|prt|gz, max_size=20000');
  $cusform->submit('Submit Customer','submit_customer_form');
  $cusform->reset('Reset', 'reset', 'onclick=[removed].reload()');
  $cusform->model('clist','new_customer');
  $this->data['form'] = $cusform->get();
  $this->data['errors'] = $cusform->errors;
  if($cusform->valid) {
   $this->upload->data($data['file']);
   $this->session->set_flashdata('mtitle', 'Success!');
   $this->session->set_flashdata('message', $this->message);
   if(!$CID) redirect('crm/customer/edit/'.$this->db->insert_id(),'refresh');
   redirect('crm/customer/edit/'.$CID,'refresh');
  }
  $this->template->write('content', $this->data['errors']);
  $this->template->write('content', $this->data['form']);
  $cusform->clear();
  //return $customer_loader['CName'];
  }

function contact_form($CID = FALSE, $CustomerID = FALSE) {
$conform = new Form();
  $this->type = array('' => '','Lead' => 'Lead','Prospect' => 'Prospect','Vendor' => 'Vendor','Customer' => 'Customer','Other' => 'Other');
  if($CID)  {
   $this->db->where('CON.ID',$CID);
   $contact_loader = $this->clist->get_contacts();
   $contact_loader = $contact_loader['raw'][0];
   $conform->fieldset('Update '. $contact_loader['FName'] .' '. $contact_loader['LName']);
   $conform->hidden('CONID',$CID);
  } else {
  $conform->fieldset('New Contact');
  }
$conform->col(400,'left','left');
$conform->text('FName|Contact_first', 'First Name', 'required|trim|max_length[30]',$contact_loader['FName'],'placeholder = Enter First Name');
$conform->text('LName|Contact_last', 'Last Name', 'required|trim|max_length[30]',$contact_loader['LName'],'placeholder = Enter Last Name');
$conform->text('WorkPhone|Contact_wphone','Work Phone','required|trim|exact_length[10]|numeric',$contact_loader['WorkPhone'],'placeholder = e.g.  6305956144');
$conform->text('CellPhone|Contact_cphone','Cell Phone','trim|exact_length[10]|numeric',$contact_loader['CellPhone']);
$conform->text('OtherPhone|Contact_ophone','Other Phone','trim|exact_length[10]|numeric',$contact_loader['OtherPhone']);
$conform->text('WorkEmail|Contact_wemail','Work Email', 'valid_email|required|trim|max_length[30]',$contact_loader['WorkEmail']);
$conform->text('OtherEmail|Contact_oemail','Other Email', 'valid_email|trim|max_length[30]',$contact_loader['OtherEmail']);
$conform->text('Website|Contact_website','Website', 'trim|max_length[45]',$contact_loader['Website'],'placeholder = e.g. www.matrixtooling.com');
$conform->col(400,'left','left');
$conform->select('Type|Contact_type', $this->type,'Type',$contact_loader['Type'],'required|trim|max_length[10]');
$conform->select('LeadSource|Contact_source', $this->clist->source_list(),'Lead Source',$contact_loader['SID'],'trim|max_length[10]');
$conform->textarea('Role|Contact_role','Role','trim',$contact_loader['Role'],'cols=25, rows=4, placeholder = Contact job postion and title here:');
$conform->textarea('Background|Contact_background','Background','trim',$contact_loader['BackGround'],'cols=25, rows=4, placeholder = Brief contact background here:');
$conform->submit('Submit Contact','submit_contact_form');
And it goes on... Any tips to improve my code are welcome. P.S. I tried the new Form(); instead of $this->form, it didn't do anything, I'll probably put it back. Thank you soo much for the help.




Theme © iAndrew 2016 - Forum software by © MyBB