Monday, February 21, 2011

How can I add to config'd Form_validation in CodeIgniter?

My situation is a bit complex. I want to create $config rules for my form_validation so that my code is cleaner, but the problem is here:

        if( $this->input->post( 'usertype' ) == "new_user" )
        {
            $this->form_validation->set_rules('new_email', 'E-mail Address', 'required|valid_email' );
            $this->form_validation->set_rules('new_firstname', 'First Name', 'required' );
            $this->form_validation->set_rules('new_lastname', 'Last Name', 'required' );
            $this->form_validation->set_rules('new_password', 'Password', 'required|matches[password_conf]' );
            $this->form_validation->set_rules('password_conf', 'Password Confirmation', 'required' );
        } else {
            $this->form_validation->set_rules('login_email', 'Login E-mail Address', 'required|valid_email' );      
            $this->form_validation->set_rules('login_password', 'Login Password Address', 'required' );
        }

What do I do if I have conditional rules?

From stackoverflow
  • if( $this->input->post( 'usertype' ) == "new_user" )
    {
        if ($this->form_validation->run('booking_create_new_account') == TRUE)
        {
            // do stuff
        }
    }
    else 
    {
        if ($this->form_validation->run('booking_create') == TRUE)
        {
            // do stuff
        }
    }
    

    This would work.

0 comments:

Post a Comment