Welcome Guest, Not a member yet? Register   Sign In
Form validation not working
#1

[eluser]mdcode[/eluser]
I appear to be having another "stupid" day, and I am unabel to get form validation running. Here is the code of my controller:
Code:
function add()
    {
        $this->auth->restrict(3);
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('title','Title','required');
        $this->form_validation->set_rules('description','Description','required');
    
        $data['site_title'] = $this->config->item('site_title');
        $data['title'] = 'Add a project';
        
            /* grab the customer list */
            if ($customers = $this->projects_model->get_all_customers())
            $data['customers'][$value['id']=0] = '';
            {
                foreach ($customers as $key=>$value)
                $data['customers'][$value['id']] = $value['name'];
            }
            
            /* grab the division list */
            if ($divisions = $this->projects_model->get_all_divisions())
            $data['divisions'][$value['id']=0] = '';
            {
                foreach ($divisions as $key=>$value)
                $data['divisions'][$value['id']] = $value['name'];
            }

// ...more form fields...
            
        /* check if the form has been submitted */
        if($this->input->post('submit_form'))
        {
            $insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() > 0)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects', 'refresh');
            }
            
        }
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        $this->template->render();
    }
My model:
Code:
/* inserting the project data */
        function insert()
        {
        $data = array(
            'customer'=>$this->input->post('customer', TRUE),
            'division'=>$this->input->post('division', TRUE),
            'dept'=>$this->input->post('dept', TRUE),
            'state_code'=>$this->input->post('state', TRUE),
// ...yada yada yada
And another except from my view:
Code:
&lt;?php echo validation_errors(); ?&gt;

<span class="fontTitle">Add a project to the Projects Database</span>
&lt;?=br(2);?&gt;
<span class="fontNormal">Use the following form to add a project to the database.&lt;?php echo br(2);?&gt;

&lt;?php echo form_open('projects/add'); ?&gt;

&lt;?php echo form_fieldset('Main Details'); ?&gt;
    <table class="table100">
        <tr>
            <td width="10%">Customer</td>
            <td width="40%">&lt;?php echo form_dropdown('customer', $customers, '', $dds); ?&gt;</td>
            <td width="10%">Division</td>
            <td width="40%">&lt;?php echo form_dropdown('division', $divisions, '', $dds); ?&gt;</td>
        </tr>

As you can see from the validation rules in the controller, I have set the title and decription fields (ordinary text fields) to be required, but on submitting the form, a record is inserted into the database. If anyone can see anything blindingly obvious, I'd appreciate a response. Thanks muchly, always impressed with the community here.
#2

[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246519569"]I appear to be having another "stupid" day, and I am unabel to get form validation running. Here is the code of my controller:
Code:
if($this->input->post('submit_form'))
        {
            $insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() > 0)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects', 'refresh');
            }
            
        }
[/quote]

I think this is inserting data without getting to the part where you run the validation.

Code:
function add()
    {
        $this->auth->restrict(3);
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('title','Title','required');
        $this->form_validation->set_rules('description','Description','required');
    
        $data['site_title'] = $this->config->item('site_title');
        $data['title'] = 'Add a project';
        
            /* grab the customer list */
            if ($customers = $this->projects_model->get_all_customers())
            $data['customers'][$value['id']=0] = '';
            {
                foreach ($customers as $key=>$value)
                $data['customers'][$value['id']] = $value['name'];
            }
            
            /* grab the division list */
            if ($divisions = $this->projects_model->get_all_divisions())
            $data['divisions'][$value['id']=0] = '';
            {
                foreach ($divisions as $key=>$value)
                $data['divisions'][$value['id']] = $value['name'];
            }

// ...more form fields...
            
        /* check if the form has been submitted */
        if($this->input->post('submit_form'))
        {
            $insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() > 0)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects', 'refresh');
            }
            
        }
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        $this->template->render();
    }
#3

[eluser]mdcode[/eluser]
Ah... nice idea, thanks, but I have just swapped around the validation stuff with the inserting stuff with no effect. Hopefully you have more ideas?
#4

[eluser]Thorpe Obazee[/eluser]
You should post how you swapped around the validation stuff...
#5

[eluser]mdcode[/eluser]
Sorry about that...

Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
            
        /* check if the form has been submitted */
        if($this->input->post('submit_form'))
        {
            $insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() === 1)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects/add', 'refresh');
            }
            
        }
        
        $this->template->render();
#6

[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246521894"]Sorry about that...

Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
            
        /* check if the form has been submitted */
        if($this->input->post('submit_form'))
        {
            $insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() === 1)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects/add', 'refresh');
            }
            
        }
        
        $this->template->render();
[/quote]

Errmm... you're still inserting... now after the form validation has run................................

Check your curly braces.

comment: you should not even be checking if the form was submitted.
hint:

Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }

You should be inserting something in the second part of the conditional statement.
#7

[eluser]mdcode[/eluser]
Then I'm a little confused -- if I don't check if the form has been submitted, it runs in an endless loop.
#8

[eluser]Thorpe Obazee[/eluser]
[quote author="mdcode" date="1246522625"]Then I'm a little confused -- if I don't check if the form has been submitted, it runs in an endless loop.[/quote]

The form validation library checks that for you here (line 279).

Code:
// Do we even have any data to process?  Mm?
        if (count($_POST) == 0)
        {
            return FALSE;
        }

Anyway... You should have something along these lines.

Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->template->write_view('content', 'default/admin/projects_add', $data);
        }
        else
        {
$insert = $this->projects_model->insert();
            
            if ($this->db->affected_rows() === 1)
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_green">The project has been added successfully.</div>');
            redirect('/projects', 'refresh');
            }
            else
            {
            $this->session->set_flashdata('flashmsg', '<div class="flash_red">Project addition has failed.  Please try again.</div>');
            redirect('/projects/add', 'refresh');
            }
        }
        
        $this->template->render();
#9

[eluser]mdcode[/eluser]
I didn't realize that, thank you! The validation appears to be working now but the errors are not showing on the page. Not sure why that is yet.




Theme © iAndrew 2016 - Forum software by © MyBB