[eluser]billmce[/eluser]
I've been using the value of the submit button to determine whether this was the first run through the form or not.
If it was the first run through the form I go and get the data to populate the form, then set the submit button value.
If the submit button had a value I know it's a re-edit and then I can validate, save etc.
Newbie alert.
The Controller function
=======================
Code:
function edit_creditor($UniqContactID = '')
{
/**
* Edit a creditor given then UniqContactID
* throws to an editing screen
*/
$this->load->helper('form');
$this->load->model('creditors_model');
$this->load->helper('form');
$this->load->library('form_validation');
$data['css'] = $this->css;
$data['images'] = $this->images;
$data['jss'] = $this->jss;
$data['base'] = $this->base;
$data['robots'] = '<meta name="robots" content="noindex,nofollow">';
$data['title']="Creditors"; //shows up on tab
$data['pagetitle']="Screen for Creditors"; //top of page body
if(empty($_POST['BtnSaveCredType']))
{
//We haven't got any data yet .. ie) first time in record.
foreach ($this->creditors_model->get_creditor($UniqContactID)->result_array() as $keyname => $cred)
{
//Break the data into individual fields
$data['CategoryID'] = $cred['CategoryID'];
$data['Company'] = $cred['Company'];
$data['SearchName'] = $cred['SearchName'];
$data['Building'] = $cred['Building'];
$data['StreetNumber'] = $cred['StreetNumber'];
$data['Unit'] = $cred['Unit'];
$data['StreetNumberSuffix'] = $cred['StreetNumberSuffix'];
$data['StreetName'] = $cred['StreetName'];
$data['StreetType'] = $cred['StreetType'];
$data['StreetDirection'] = $cred['StreetDirection'];
$data['Municipality'] = $cred['Municipality'];
$data['ProvinceState'] = $cred['ProvinceState'];
$data['PostalCodeZip'] = $cred['PostalCodeZip'];
$data['Country'] = $cred['Country'];
$data['LTCCode'] = $cred['LTCCode'];
$data['GovernmentCode'] = $cred['GovernmentCode'];
$data['PrivacyPolicyExempt'] = $cred['PrivacyPolicyExempt'];
$data['ProofOfClaimRequired'] = $cred['ProofOfClaimRequired'];
$data['AdditionalInformation'] = $cred['AdditionalInformation'];
}
$data['BtnSaveCredType'] = 'Save Changes';
// now go in and bring up the form.
$this->load->view('header_view',$data);
$this->load->view('menu_view');
$this->load->view('creditor_edit_view', $data);
$this->load->view('footer_view',$data);
}
else
{
echo "Button not empty -- s/b saving";
//NOTE: 'set_value' ONLY works on fields that are being validated.
// I think you must have a rule for each field you intend to use with set_value
$this->form_validation->set_rules('CategoryID','CategoryID', 'required');
$this->form_validation->set_rules('Company', 'Company', 'required|min_length[80]');
$this->form_validation->set_rules('SearchName', 'SearchName', 'required');
$this->form_validation->set_rules('Building','Building', 'required');
$this->form_validation->set_rules('StreetNumber','StreetNumber', 'required');
$this->form_validation->set_rules('Unit','Unit', 'required');
$this->form_validation->set_rules('StreetNumberSuffix','StreetNumberSuffix', 'required');
$this->form_validation->set_rules('StreetName','StreetName', 'required');
$this->form_validation->set_rules('StreetType','StreetType', 'required');
$this->form_validation->set_rules('StreetDirection','StreetDirection', 'required');
$this->form_validation->set_rules('Municipality','Municipality', 'required');
$this->form_validation->set_rules('ProvinceState','ProvinceState', 'required');
$this->form_validation->set_rules('PostalCodeZip','PostalCodeZip', 'required');
$this->form_validation->set_rules('Country','Country', 'required');
$this->form_validation->set_rules('LTCCode','LTCCode', 'required');
$this->form_validation->set_rules('GovernmentCode','GovernmentCode', 'required');
$this->form_validation->set_rules('PrivacyPolicyExempt','PrivacyPolicyExempt', 'required');
$this->form_validation->set_rules('ProofOfClaimRequired','ProofOfClaimRequired', 'required');
$this->form_validation->set_rules('AdditionalInformation','AdditionalInformation', 'required');
$data['BtnSaveCredType'] = 'Save Revised Changes';
if ($this->form_validation->run() == FALSE)
{
$this->load->view('header_view',$data);
$this->load->view('menu_view');
$this->load->view('creditor_edit_view2', $data);
$this->load->view('footer_view',$data);
}
else
{
// call model to save data here.
}
};
}