Welcome Guest, Not a member yet? Register   Sign In
best practices on Form Reuse - keeping all forms in a central location with validation
#29

[eluser]chuckleberry13[/eluser]
Yes its using ME. Sorry if that was a little vagiue.

So here is the controller containing the form.

We will call it Form Controller
Code:
class Form extends Controller {
    
    function Form()
    {
       parent::Controller();
    }


function signup($partial = NULL)
    {    
        $this->load->library('validation');
        
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        
            $rules['first_name'] = "required|xss_clean|trim";
            $rules['last_name'] = "required|xss_clean|trim";
            $rules['phone'] = "required|xss_clean|trim";
            $rules['username'] = "required|xss_clean|trim|min_length[4]|max_length[25]|callback_check_username";
            $rules['password'] = "required|xss_clean|trim|min_length[4]";
            $rules['rpassword'] = "required|xss_clean|trim|matches[password]";
            $rules['email'] = "required|xss_clean|trim|valid_email|trim|callback__check_email";
            $rules['remail'] = "required|xss_clean|trim|valid_email|matches[email]";
            
        $this->validation->set_rules($rules);

            $fields['first_name'] = 'First Name';
            $fields['last_name'] = 'Last Name';
            $fields['phone'] = 'Phone';
            $fields['username'] = 'Username';
            $fields['password'] = 'Password';
            $fields['rpassword'] = 'Password Confirmation';
            $fields['email'] = 'Email Address';
            $fields['remail'] = 'Email Confirmation';
        
        $this->validation->set_fields($fields);
    
        if($this->validation->run() == FALSE)
        {
            $this->set_title('Account - Create Account!');
            $this->set_header('Create Your PyrBlu Account!','FEBD23');
            
            $this->add_js(array('jquery.maskedinput','jquery.passmeter','forms','create_account','jquery.validate'));
            $subdata['terms'] = $this->load->view('account/terms_txt', '', TRUE);    
            $data['body'] = $this->load->view('account/signup_form', $subdata, TRUE);
        }
        else
        {
            $this->user->signup();
            if($partial)
                redirect($partial);
            else
            {
                $this->set_title('Account - Creation Success!');
                $this->set_header('Account Successfully Created!','#9fd718');
                $subdata['user_data'] = $this->user->data;
                $data['body'] = $this->load->view('account/signup_success', $subdata, TRUE);
            }
        }
        
        if($partial)
            return $data['body'];
        else
            $this->display($data);        
    }

Now here is a view that I want to include this form in but only as a widget and on this view when the form passes validation I want it to redirect to page/newsletter.

Code:
&lt;?= modules::run('form', 'page/newsletter', 'signup') ?&gt;

So my controller 'Form' holds the validation rules, the callback functions, handles the validation as well as serve up the form. It then redirects if the partial variable is present. Since I actually use this form via the URL (forms/signup) you'll notice the controller is set to spit out its own success page if there is not redirect variable present.

Did that make any sense? This seemed to me the easiest way to reuse this form. What do you think?


Messages In This Thread
best practices on Form Reuse - keeping all forms in a central location with validation - by El Forum - 07-25-2008, 10:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB