Welcome Guest, Not a member yet? Register   Sign In
form rules/values: helper, controller or model?
#1

[eluser]domsworkshop[/eluser]
Howdy folks, I've been developing with CodeIgniter for a few months and developed my site using this wonderful framework, and I know just enough PHP/MySQL to be dangerous.

Now that I've gained a certain comfort level with the framework I'm debugging and improving code, while also trying to adapt each structure (ie user structure, blog structure etc) to be more modular, allowing me to offer sets of files for download so that other beginning developers can benefit from some of the things I've figured out.

One of these is a very basic helper that stores arrays of form fields and form rules, with qualifier loops, for the different structures. Currently everything is stored all within the same helper - comments, users, invoices, etc. but I'd like to break it apart (in the interests of above goals and also to make each structure more lightweight).

My question is this: which of the following options would be considered best practice?
- separate each set of rules into its own helper; this seems the best bet but would mean a lot of helpers; is that bad?
- rewrite the arrays as private functions within the controller; this sounds nice, but complicates matters if I would like other controllers to be able to use these values (or does it?)
- place the arrays as private functions within the model; this seems feasible because the fields for each structure are ultimately used by the corresponding model, and could be called by different controllers, but the functions themselves wouldn't actually interact with the database. I like this idea best but don't know if it's a bad practice to have functions like that cluttering up my models.

Thanks for any advice you can offer!
#2

[eluser]domsworkshop[/eluser]
Forgot to include a code example. Here's what I have in my current values helper.

Code:
if (!function_exists('form_rules')) {

    function form_rules($options = array()) {
    
        $rules_list = array (
        
            //User data
            'user_name' => array('field' => 'user_name', 'rules' => 'trim|required|min_length[3]|max_length[16]|xss_clean'),
            'user_email' => array('field' => 'user_email', 'rules' => 'trim|required|valid_email'),
            'user_full_name' => array('field' => 'user_full_name', 'rules' => 'trim|xss_clean'),
            'user_password' => array('field' => 'user_password', 'rules' => 'trim|required|min_length[5]|max_length[20]'),
            'user_confirm' => array('field' => 'user_confirm', 'rules' => 'trim|required|matches[user_password]'),
            'user_create_date' => array('field' => 'user_create_date', 'rules' => 'required'),
            'user_current' => array('field' => 'user_current', 'rules' => 'trim|required'),
            
            //Contact data
            'contact_subject' => array('field' => 'contact_subject', 'rules' => 'trim|required|xss_clean'),
            'contact_body' => array('field' => 'contact_body', 'rules' => 'trim|required|xss_clean'),
            'contact_name' => array('field' => 'contact_name', 'rules' => 'trim|required|xss_clean'),
            'contact_email' => array('field' => 'contact_email', 'rules' => 'trim|valid_email'),
            'contact_phone' => array('field' => 'contact_phone', 'rules' => 'trim|xss_clean'),
            
            //Blog data
            'blog_id' => array('field' => 'blog_id', 'rules' => 'required'),
            'blog_title' => array('field' => 'blog_title', 'rules' => 'trim|required'),
            'blog_contents' => array('field' => 'blog_contents', 'rules' => 'trim|required'),
            
            //Comments data
            'comment_title' => array('field' => 'comment_title', 'rules' => 'trim|xss_clean'),
            'comment_author' => array('field' => 'comment_author', 'rules' => 'trim|xss_clean|max_length[30]'),
            'comment_contents' => array('field' => 'comment_contents', 'rules' => 'trim|required|xss_clean|max_length[5000]'),
            
            //Authentication
            'spam_block' => array('field' => 'spam_block', 'rules' => 'required|spam_check')
        );
        
        foreach ($options as $field) {
            if(isset($rules_list[$field])) {
                $validation_rules[$field] = $rules_list[$field];
            }
        }
        
        return $validation_rules;
        
    }
    
}
#3

[eluser]Vinzent Zeppelin[/eluser]
I think validation rules would be best placed along with the data to which they apply, in the corresponding models. Take a look at the DMZ library; it includes validation rules and relations in private attributes, and automatically validates data when it is entered.
#4

[eluser]domsworkshop[/eluser]
I'm glad you agree...I just tried integrating the user rules and field values into my user model and it looks like much cleaner code and much easier for data prep. Thanks!
#5

[eluser]Vega[/eluser]
Check out: saving sets of rules to a config file

You can then run the validation rules above like:

Code:
if ($this->form_validation->run('post') !== TRUE)
{
  // load view for error
}
else
{
  // success: add to db/redirect etc
  $this->some_model->create();
}

Personally I don't like putting validation or anything else in the model or else it doesn't really abide by the MVC principal. It should be database work only.
#6

[eluser]domsworkshop[/eluser]
Initially when I wrote this helper I was looking at that document, but it really only seemed viable for validation rules (at least for my current proficiency level); I also want a DRY solution for outputting form fields, and database qualifiers to prevent unwanted SQL insertion, and it seems that grouping three utility functions for each of these things together in one file (whether helper, controller, model, etc.) makes things a little bit more neat and tidy without code scattered across too many files.




Theme © iAndrew 2016 - Forum software by © MyBB