donglu5041 2012-02-23 00:12
浏览 44
已采纳

如何在CodeIgniter中的表单验证中设置自定义过滤器?

I am using CodeIgniter's form validation. Here is an example:

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

The documentation say:

Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc.

What if I want the value to pass through my own custom filter? For example, I would like the value to be cleaned of "badWord".

function curseWordRemove($original = '') {
    return str_replace('badWord', '', $original);
}

CodeIgniter already provides ways to do custom validation, but not custom filters. The custom validation only returns true or false, not the filtered string.

function isPolite($string = '') {
    if (strpos($string, 'badWord') !== false) {
        $this->form_validation->set_message(
            'fieldName',
            'contains a very bad word'
        );
        return false;
    } else {
        return true;
    }
}
  • 写回答

1条回答 默认 最新

  • duanji1610 2012-02-23 03:19
    关注

    Jojo, you must have missed it the userguide, its called a callback, and here is the documentation.

    An Example:

    <?php
    
    class Form extends CI_Controller {
    
        public function index()
        {
            $this->load->helper(array('form', 'url'));
    
            $this->load->library('form_validation');
    
            $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
    
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('myform');
            }
            else
            {
                $this->load->view('formsuccess');
            }
        }
    
        public function username_check($str)
        {
            if ($str == 'test')
            {
                $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
    
    }
    ?>
    

    Basically, create a validation called callback_check_bad_words and a matching function in your controller called check_bad_words($value). Return a boolean as a result (as the result goes back to the validation).

    Since you can only pass back a boolean, you need to either use a global variable, OR run the 'sanitization' of your word later on, you don't need it in validation UNLESS you want to stop it from submission.

    If your intent is to sanitize the input for bad words, just do it, don't validate for it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成