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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动