Welcome Guest, Not a member yet? Register   Sign In
Help with Form Validation
#2

[eluser]TheFuzzy0ne[/eluser]
Firstly, you should only really run the validation when you know the form is being submitted. My forms tend to have a button with the name attribute set to "submit". So then I can do something like this in my code:
Code:
if ($this->input->post('submit') && $this->form_validation->run())
{
    // ...

I would probably write your method something like this:
Code:
<?php
function index()
{
    # Set up rules to validate the getting started form

    $rule = array(
            array(
                'field'   => 'user_first_name',
                'label'   => 'First Name',
                'rules'   => 'required'
            ),
            array(
                'field'   => 'user_last_name',
                'label'   => 'Last Name',
                'rules'   => 'required'
            ),
            array(
                'field'   => 'user_email',
                'label'   => 'Email',
                'rules'   => 'required|valid_email'
            ),  
            array(
                'field'   => 'user_phone',
                'label'   => 'Phone',
                'rules'   => 'required'
            )
        );
    
    $this->form_validation->set_rules($rules);
    
    # Check if the form is being submitted. If it is, run validation

    if ($this->input->post('submit') && $this->form_validation->run() === TRUE)
    {
        # If the validation has passed, load another view and return/exit,
        # or redirect to another page.
    }
    
    # I like to keep my data array declaration down here, as often, things like
    # the title are changed dynamically depending on the outcome of the above logic.
    # This doesn't mean you should, however.

    $data['title'] = "Order Stuff Here";
    $data['heading'] = "Let's Get Started";
    
    $this->load->view('order_index', $data); // Load this view by default
}

Hope this helps.

EDIT: And it's really quite refreshing to see someone declaring a doctype in their view file, especially XHTML Strict. :cheese:


Messages In This Thread
Help with Form Validation - by El Forum - 03-07-2009, 07:16 AM
Help with Form Validation - by El Forum - 03-07-2009, 07:37 AM
Help with Form Validation - by El Forum - 03-07-2009, 08:08 AM
Help with Form Validation - by El Forum - 03-07-2009, 08:29 AM
Help with Form Validation - by El Forum - 03-07-2009, 09:39 AM
Help with Form Validation - by El Forum - 03-07-2009, 09:43 AM
Help with Form Validation - by El Forum - 03-07-2009, 09:47 AM
Help with Form Validation - by El Forum - 03-07-2009, 02:03 PM
Help with Form Validation - by El Forum - 03-07-2009, 02:17 PM
Help with Form Validation - by El Forum - 03-07-2009, 02:36 PM
Help with Form Validation - by El Forum - 03-07-2009, 02:53 PM
Help with Form Validation - by El Forum - 03-11-2009, 12:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB