Welcome Guest, Not a member yet? Register   Sign In
Edit, validate, re-edit issue using set_value with optional parameter
#21

[eluser]bretticus[/eluser]
I SEE THAT YOU SOLVED THIS. I JUST FINISHED MY EXAMPLE FOR YOU...

This is along the lines of my suggestion:

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

    $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');


    //initialize your variables

    if ( $this->input->post('CategoryID') !== FALSE ) {            

        $data['BtnSaveCredType'] = 'Save Revised Changes';

        // let's not load them again from the database but we must initialize them.
        // set_value will use the POST variables if detected.

        $data['CategoryID'] = '';
        $data['Company'] = '';
        $data['SearchName'] = '';
        $data['Building'] = '';
        $data['StreetNumber'] = '';
        $data['Unit'] = '';
        $data['StreetNumberSuffix'] = '';
        $data['StreetName'] = '';
        $data['StreetType'] = '';
        $data['StreetDirection'] = '';
        $data['Municipality'] = '';
        $data['ProvinceState'] = '';
        $data['PostalCodeZip'] = '';
        $data['Country'] = '';
        $data['LTCCode'] = '';
        $data['GovernmentCode'] = '';
        $data['PrivacyPolicyExempt'] = '';
        $data['ProofOfClaimRequired'] = '';
        $data['AdditionalInformation'] = '';
    } else {

        $data['BtnSaveCredType'] = 'Save Revised Changes';

        // original page load, let's get the database data
        $creditors = $this->creditors_model->get_creditor($UniqContactID)->row_array();
        // array keys built form row_array must match exactly to avoid the undefined notice.
        $data = array_merge($data, $creditors);
    }

    //okay, now we've setup variables let's actually validate the form and decide if we should proceed to
    // really save reuslts.

    if ($this->form_validation->run() === FALSE) {
        $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 {
        // call model to save data  here.

        redirect('path/to/success/page');
    }
}

Glad you got it working.
#22

[eluser]bretticus[/eluser]
[quote author="billmce" date="1269043040"]

The crux of the problem is that set_value expects that both of the values passed to it have some value.

On the second pass, after validation failure, there is no value in the variables because I only got the data once .. the first pass when I actually need it to populate the view.
[/quote]

Actually, had you read my original post better, you would have noticed I answered that question.

Quote:...Make sure you initialize these variables so that they are empty arrays elements at least to avoid Undefined Variable notices (not errors.)

[quote author="billmce" date="1269043040"]
I'd suggest that set_values optional parameter behave differently ... but hey, it's now working.[/quote]

This isn't a set_values() issue. This is the normal behavior for PHP when you specify a variable that doesn't exist. AND, when you have php error handling set to show notices (which, again, are not actually errors.)

Again, glad you go it working.
#23

[eluser]billmce[/eluser]
I'm glad you posted that because I was wondering about setting the values of the field to blank to initialize them each pass ... thinking that would eliminate the need to repopulate.

Thanks.
#24

[eluser]billmce[/eluser]
Yes, I should've caught onto it sooner. It's just good practice to initialize variables.

Thanks to all ... the lesson is pounded into my thick skull.




Theme © iAndrew 2016 - Forum software by © MyBB