简体   繁体   中英

Codeigniter form validation for big form (28 fields)

I have form consists of 28 fields (client requirement). I am using Codeigniter framework. I have to type 28 lines of code for form validation. Is there anything else I can do like creating helper for form validation. Any help would be appreciated. Thanks

There is no standard here or a right way to do it and your best option is to create a validation rule for every single input of them in your validation array and maybe you can make a check to categorize some of them like "fname, lname, ..." those have the same validations, but that won't save you anything just added overhead.

But maybe your best shot is to create a function in your base_controller like this:

    function set_validation_rules()
    {
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'firstname',
                'label' => $this->lang->line($line.'firstname'),
                'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
            ),
            array(
                'field' => 'lastname',
                'label' => $this->lang->line($line.'lastname'),
                'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
            ),
            array(
                'field' => 'password',
                'label' => $this->lang->line($line.'password'),
                'rules' => 'trim|required|min_length['.$min.']|max_length['.$max.']'
            ),
            array(
                'field' => 'passconf',
                'label' => $this->lang->line($line.'password_confirm'),
                'rules' => 'trim|matches[password]'
            )
        );
        $this->form_validation->set_rules($config);
    }

or create a helper as you suggested, but the most important step is to make a convention for yourself for var/inputs naming and make it a habit so inputs from different view have the same names and pass all inputs to this function for validation after modifying by adding a switch cases to it so every firstname has its own rules and every password has its own and so on, hope you got the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM