dongwei4103 2018-11-21 14:30
浏览 42

大表格的Codeigniter表单验证(28个字段)

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

  • 写回答

1条回答 默认 最新

  • du1843 2018-11-21 22:43
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题