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.

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

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系