Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Moving from validation to form_validation libraries
#1

[eluser]froddd[/eluser]
Hey all,

I'm having an issue updating some code to use the 'form_validation' library instead of the deprecated 'validation' one...

The main problem here is that I'm loading the form itself as a separate view, as I'm keen on using the same HTML for both adding new records and editing existing ones.

With the old 'validation' library, my code would look similar to this in the controller:
Code:
function add()
    {        
        $client = new Client();
        
        $this->load->library('validation');
        
        // general validation stuff
        $this->validation->set_error_delimiters('<label class="errorbg">', '</label>');
        
        // set rules
        $rules['name'] = "required";
        $rules['email'] = "valid_email";
        $this->validation->set_rules($rules);
        // set messages
        $fields['name'] = 'Name';
        $fields['email'] = 'Email';
        $this->validation->set_fields($fields);
        
        if( $this->validation->run() ){
            // process the data
            $client->name = $this->input->post('name');
            $client->email = $this->input->post('email');
            
            if( $client->save() ){
                $this->session->set_flashdata('action', $client->name . ' has been added to your clients');
                redirect('/clients');
            }
        } else {
            $formdata['error'] = '';
        }
        
        $formdata['form_action'] = 'add';
        $formdata['validation'] = $this->validation;

        // load the form into the view
        $data['form'] = $this->load->view('clients/form', $formdata, true);
    
        // general page variables
        $data['page_title'] = 'Clients';
        
        $this->load->view('inc/header', $data);
        $this->load->view('clients/add', $data);
        $this->load->view('inc/footer');
    }
And the view for the form:
Code:
&lt;?=$error?&gt;

&lt;form action="/clients/&lt;?=$form_action?&gt;" method="post"&gt;

    <p><label for="name">Name</label> &lt;input id="name" name="name" type="text" value="&lt;?=$validation-&gt;name?&gt;" />&lt;?=$validation->name_error?&gt;</p>
    
    <p><label for="email">Email</label> &lt;input id="email" name="email" type="text" value="&lt;?=$validation-&gt;email?&gt;" />&lt;?=$validation->email_error?&gt;</p>
    
    <p><button type="submit">Save client</button> - <a href="/clients">Cancel</a></p>
    
&lt;/form&gt;

As I was then able to pass $this->validation as a view variable I could then show the values and errors as validation properties. However, with the 'form_validation' library, I cannot do so as to show values and errors I need to use set_value() and form_error()... Which I can't seem to be able to pass to the 'second-level' form view.

Any idea? I've quickly copy-pasted some code while trying to remove unneeded fields, so if there's any typo in this I'm sorry, ignore them!

Thanks for any help :-P
#2

[eluser]froddd[/eluser]
Dang, silly mistake... Was not using applying the rules to the validation process ($this->form_validation->set_rules($config)) -- I'd commented the line out while debugging something else...

So it seems the new form_validation library is genius after all!!!

Silly me :-P




Theme © iAndrew 2016 - Forum software by © MyBB