Welcome Guest, Not a member yet? Register   Sign In
how to implement templating
#1

[eluser]misterdonut[/eluser]
i have a template and i would like to know how to properly use it together with a validation class.

Code:
function index()
    {

        $css = "<link href='".base_url()."assets/css/login.css' rel='stylesheet' type='text/css' />";
        $css .= "<link href='".base_url()."assets/css/forms.css' rel='stylesheet' type='text/css' />";
        $login = form_open('user/login')."
                    <div id='login'>
                        <div>
                        <ul>
                            <li>
                                <div>
                                <h3 for='username'>Username</h3>                                
                                &lt;input id='input' type='text' name='username'&gt;
                                </div>
                            </li>                            
                            <li>
                                <div>
                                <h3 for='password'>Password</h3>                                
                                &lt;input id='input' type='password' name='password'&gt;
                                </div>
                            </li>
                            <li>
                                <div>                            
                                &lt;input type='image' id='loginsubmit' src='".base_url()."assets/images/login.png' value='Log me in'    &gt;
                                </div>
                            </li>
                        
                        </ul>
                        </div>
                    </div>
                &lt;/form&gt;
                ";
        $this->template->write('title', 'Portal Login');
        $this->template->write('head', $css);
        $this->template->write('content', $login);
        $this->template->render($region = NULL, $buffer = FALSE, $parse = FALSE);
    }

please give me comments on how to implement my validation together with templating. i'm new to both libraries TIA.

my problem is how to get the previous value of the region 'content' and reuse it and repopulate my form.

Code:
function login()
    {
        $this->load->library('validation');
        $rules['username'] = "required|xss_clean";
        $rules['password'] = "required|md5";    
        $this->validation->set_rules($rules);        
        
        if ($this->validation->run() == FALSE){
            //error msg with the form and previous values
            //$this->template->render($region = NULL, $buffer = FALSE, $parse = FALSE);
            echo "fail::";
        }else{        
            //login
            redirect('user/main');
        }    
    }
#2

[eluser]jbrozz[/eluser]
wow, this is a very simple way to implementing HTML form into source code.

Your problem consist of populate the input field, right?
#3

[eluser]misterdonut[/eluser]
i need to know how to get my forms when the validation fails together with the values inputted by the user. will i type it again? i think it will mess up my code, just thinking how to minimize my code Big Grin i like your avatar.
#4

[eluser]jbrozz[/eluser]
LOL, my avatar is really cool!

Ah, you need a single field error?!?! right? Smile

You can use.... for example:

($rules['username'] == false) echo "error 1";
($rules['password'] == false) echo "error 2";

a single condition for single field! Not good idea? Wink
#5

[eluser]misterdonut[/eluser]
ahh never mind got it working.
thank you Big Grin

btw where do you get avatars like that?
#6

[eluser]Colin Williams[/eluser]
You can follow the same guide of using Validation in the User Guide. The only difference is that you're using the Template library instead of vanilla Views.

Code:
function login()
    {
        $this->load->library('validation');
        $rules['username'] = "required|xss_clean";
        $rules['password'] = "required|md5";    
        $this->validation->set_rules($rules);        
        
        if ($this->validation->run() == FALSE){
            //error msg with the form and previous values
            $this->template->write('content', $this->validation->error_string);
            $this->template->write_view('content', 'login_form');
            $this->template->render();
        }else{        
            //login
            redirect('user/main');
        }    
    }

Also, I would suggest keeping that form code it it's own view file and use the write_view() method of Template.

Also, you don't need to call render() like that. You can simply do $this->template->render(), and just pass in the other parameters when necessary.
#7

[eluser]misterdonut[/eluser]
thank you sir. Big Grin i would do that.

thats what missing a view file Smile)

thank you thank you




Theme © iAndrew 2016 - Forum software by © MyBB