Welcome Guest, Not a member yet? Register   Sign In
Problem with loding views and form validation
#1

[eluser]cpass78[/eluser]
Hello,

I'm trying to submit a form that is contained in a view file which gets loaded via a central template file. The form/view is loading and i can submit the form but none of the validation seems to be working. I'm wondering but not sure if it's because of my implementation?

Here is my setup:

Controller: loads correctly
Code:
function addCompany()
    {
        $this->load->library('form_validation');
        $this->data['pagetitle'] = "Add New Company";
        $this->data['html'] = $this->load->view('theme/content/add_new_company', '', true);        
        
        $this->form_validation->set_rules('CompanyName','Company Name','required|trim|max_length[100]');            
        $this->form_validation->set_rules('BillingAddress1','Street Address','required|trim|max_length[100]');            
        $this->form_validation->set_rules('BillingAddress2','Suite','trim|max_length[100]');            
        $this->form_validation->set_rules('City','City','required|trim|max_length[30]');            
        $this->form_validation->set_rules('State','State','required|trim|max_length[2]');            
        $this->form_validation->set_rules('Zip','Zip Code','required|trim|max_length[10]');            
        $this->form_validation->set_rules('Phone','Phone','required|trim|max_length[20]');            
        $this->form_validation->set_rules('fax','Fax','trim|max_length[20]');            
        $this->form_validation->set_rules('WebAddress','Web Address','trim|max_length[100]');            
        $this->form_validation->set_rules('ClientTypeID','Company Type','required|trim|is_numeric|max_length[3]');            
        $this->form_validation->set_rules('ClientActivityID','Company Activity Level','trim|is_numeric|max_length[3]');            
        $this->form_validation->set_rules('ClientSourceID','Lead Source','required|trim|is_numeric|max_length[3]');            
        $this->form_validation->set_rules('Notes','Company Notes','trim|max_length[500]');
            
        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
    
        if ($this->form_validation->run() == FALSE) // validation hasn'\t been passed
        {
            $this->load->view( 'template', $this->data ); //inject the form into the main content
        }
        else // passed validation proceed to post success logic
        {
            // build array for the model
            $form_data = array(
            'CompanyName' => set_value('CompanyName'),
            'BillingAddress1' => set_value('BillingAddress1'),
            'BillingAddress2' => set_value('BillingAddress2'),
            'City' => set_value('City'),
            'State' => set_value('State'),
            'Zip' => set_value('Zip'),
            'Phone' => set_value('Phone'),
            'fax' => set_value('fax'),
            'WebAddress' => set_value('WebAddress'),
            'ClientTypeID' => set_value('ClientTypeID'),
            'ClientActivityID' => set_value('ClientActivityID'),
            'ClientSourceID' => set_value('ClientSourceID'),
            'Notes' => set_value('Notes')
            );
            // run insert model to write data to db
            if ($this->sales_model->newCompany($form_data) == TRUE) // the information has therefore been successfully saved in the db
            {
                $this->data['message'] = 'Company saved successfully';
                $this->load->view( 'template', $this->data );   // or whatever logic needs to occur
            }
            else
            {
            $this->data['message'] = 'An error occurred saving your information. Please try again later';
            $this->load->view( 'template', $this->data );
            // Or whatever error handling is necessary
            }
        }
    }
#2

[eluser]cpass78[/eluser]
Think I figured it out, had to move my data['html'] inside the $this->form_validation->run() block.




Theme © iAndrew 2016 - Forum software by © MyBB