Welcome Guest, Not a member yet? Register   Sign In
Where do you store your data validation declarations?
#1

[eluser]housecor[/eluser]
I have a very large controller function that I'm refactoring, and I've noticed a common theme with my large controller functions is a big section of validation declarations like this:

Code:
$rules['parentcategory'] = "integer|max_length[20]|required";
            $rules['name']                = "trim|required|min_length[2]|max_length[50]|xss_clean";
            $rules['pagetitle']            = "min_length[2]|max_length[100]|xss_clean";
            $rules['intro']             = "maxlength[15000]|xss_clean";
            $rules['description']        = "maxlength[15000]|xss_clean";
            $rules['url']                 = "maxlength[255]|xss_clean";
            $rules['metadescription']    = "maxlength[15000]|xss_clean";
            $rules['metakeywords']         = "maxlength[15000]|xss_clean";
            $rules['zoomwords']         = "maxlength[15000]|xss_clean";
            $rules['zoompageboost']        = "maxlength[2]|alpha_num";
            $rules['status']            = "required|integer|maxlength[1]";
            $rules['imagealttag']        = "maxlength[255]|xss_clean";
            $rules['icon1alttag']        = "maxlength[255]|xss_clean";
            $rules['icon2alttag']        = "maxlength[255]|xss_clean";
            $rules['homepageorder']        = "integer|max_length[2]";
            $this->validation->set_rules($rules);
    
            $fields['parentcategory']    = 'Parent Category';
            $fields['name']             = 'Name';
            $fields['pagetitle']        = 'Page Title';
            $fields['intro']            = 'Introduction Text';
            $fields['description']        = 'Description';
            $fields['url']                = 'Display URL';
            $fields['metadescription']    = 'Meta Description';
            $fields['metakeywords']        = 'Meta Keywords';
            $fields['zoomwords']        = 'Zoom Words';
            $fields['zoompageboost']    = 'Zoom Pageboost';
            $fields['status']            = 'Status';
            $fields['imagealttag']        = 'Category Banner Image Name';
            $fields['icon1alttag']        = 'Category Icon 1 Image Name';
            $fields['icon2alttag']        = 'Category Icon 2 Image Name';
            $fields['homepageorder']    = 'Home Page Display Order';
            $this->validation->set_fields($fields);

So to make my controllers easier to read, I'd like to move these validation declarations to a separate function. Where is the most logical place to put this?

A couple ideas:
-A private function in the controller
-A helper

But how do I assure these values are still in scope for use in my views/models by calling $this->validation->myfield if I move this code out of the controller function?

Thanks in advance!
#2

[eluser]internut[/eluser]
You should first look at the new method of form validation. I believe you're using the old which is deprecated.

http://ellislab.com/codeigniter/user-gui...ation.html

http://ellislab.com/codeigniter/user-gui...e_170.html

Its a pretty simple transition. It will shorten up your code.




Theme © iAndrew 2016 - Forum software by © MyBB