【问题标题】:How can I validate a "does not match" type using CodeIgniter's Form Validation class?如何使用 CodeIgniter 的表单验证类验证“不匹配”类型?
【发布时间】:2011-02-10 03:24:40
【问题描述】:

我想确保某个文本字段不包含特定值。我有什么办法可以使用 CI 的表单验证类来做到这一点,还是我必须为它编写自己的扩展?

【问题讨论】:

    标签: codeigniter validation


    【解决方案1】:

    我会扩展表单验证类: http://codeigniter.com/user_guide/general/creating_libraries.html

    类似

    <?
    class MY_Form_validation extends CI_Form_validation {
      function __constuct() {
        parent::__constuct();
      }
      function isnt($str,$field){
        $this->CI->form_validation->set_message('isnt', "%s contains an invalid response");
        return $str!==$field;
      }
    }
    ?>
    

    您的验证规则类似于

    trim|alpha_numeric|isnt[invalid value]
    

    或者,您可以创建回调函数而不是扩展类。 CI 用户指南的表单验证部分有一个相关示例: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

    【讨论】:

    • 不错的答案......但有两件事。首先,$this-&gt;CI-&gt;form_validation 是不必要的,因为您已经在该类中工作。只需执行$this-&gt;set_message()。其次,当你扩展一个类时,不需要构造函数,除非你在构造函数中做一些事情。在这种情况下,在这里调用它与不调用它是一样的。
    【解决方案2】:

    我同意 Billiam 的观点,您应该扩展 Form_validation 类

    我发现人们更有可能想要验证可能的字符串值的白名单而不是黑名单。例如,您知道您的 'shirt_size' 字段应该只返回字符串值:'xl'、'l'、'm'、's'。我的解决方案是处理这两种情况。

    我在 MY_From_validation 中使用这些方法:

    
    
    
        /**
         * ENUM
         * The submitted string must match one of the values given
         *
         * usage:
         * enum[value_1, value_2, value_n]
         *
         * example (any value beside exactly 'ASC' or 'DESC' are invalid):
         * $rule['order_by'] = "required|enum[ASC,DESC]";
         * 
         * example of case-insenstive enum using strtolower as validation rule
         * $rule['favorite_corey'] = "required|strtolower|enum[feldman]";
         *
         * @access    public
         * @param     string $str the input to validate
         * @param     string $val a comma separated lists of values
         * @return    bool
         */
        function enum($str, $val='')
        {
    
            if (empty($val))
            {
            return FALSE;
            }
    
            $arr = explode(',', $val);
            $array = array();
            foreach($arr as $value)
            {
            $array[] = trim($value);
            }
    
            return (in_array(trim($str), $array)) ? TRUE : FALSE;
        }
    
    
        // --------------------------------------------------------------------
    
        /**
         * NOT ENUM
         * The submitted string must NOT match one of the values given
         *
         * usage:
         * enum[value_1, value_2, value_n]
         *
         * example (any input beside exactly 'feldman' or 'haim' are valid):
         * $rule['favorite_corey'] = "required|not_enum['feldman','haim']";
         *
         * @access   public
         * @param    string $str the input to validate
         * @param    string $val a comma separated lists of values
         * @return   bool
         */
        function not_enum($str, $val='')
        {
            return ($this->enum($str,$val) === TRUE)? FALSE : TRUE;
        }
    

    使用 Billiam 的示例,验证规则不允许字符串“无效值”类似于:

    trim|alpha_numeric|not_enum[invalid value]
    

    【讨论】:

      【解决方案3】:

      实际上,用户指南中针对这个问题给出了一个相当简单的示例——V2V3

      查找“回调:您自己的验证函数”部分。在示例中,它在用户名字段中检查单词“test”,如果找到该值,则返回自定义错误。

      在您的控制器中,将“用户名”规则更改为:

      $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
      

      然后将一个名为 username_check 的新函数添加到您的控制器:

      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;
          }
      }
      

      鲍勃是你的叔叔……

      【解决方案4】:

      CodeIgniter 的表单验证类几乎可以调用规则集中任何声明的 PHP 函数。所以我会简单地声明一个这样的函数:

      class yourController {
          function someFunction() {
              $this->form_validation->set_rules('the_field_you_want_to_check', 'The Field Name', 'trim|myvalfunc[not this value]|xss');
          }
      }
      function myvalfunc($formvalue, $notallowed) {
          $this->CI->form_validation->set_message('myvalfunc', "%s is not allowed");
          return $formvalue !== $nowallowed;
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-13
        • 1970-01-01
        • 1970-01-01
        • 2011-01-14
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        相关资源
        最近更新 更多