【问题标题】:How to customize form validation errors in codeIgniter如何在 codeIgniter 中自定义表单验证错误
【发布时间】:2012-06-30 04:59:45
【问题描述】:

codeIgniter 中是否有我可以编辑的文件,以便我可以自定义表单验证消息?

我只想将它们放在项目符号列表中以减少空间。

这是我用来输出错误消息的代码:

<div class="alert <?php echo $alert['alert_type']; ?> min-form">
        <button type="button" class="close" data-dismiss="alert">x</button>
        <h4><?php echo $alert['main_message']; ?></h4> 
        <?php echo $alert['sub_message']; ?>
</div>

基本上,$alert['sub_message'] 只是从 CodeIgniter 的 validation_errors() 函数获取数据,该函数从表单输出验证错误。

【问题讨论】:

  • 如果还没有,我建议也使用客户端验证层,因此只有在他们仍然能够提交表单时才能看到 CodeIgniter 层。
  • 另外,我会参考 CI 文档:Callbacks: Your own Validation Functions
  • @JaredFarrish 谢谢我已经使用 liveValidation livevalidation.com进行客户端验证
  • 你看过关于自定义验证的文档吗?
  • 是的,我找到了。只需编辑 system/libraries 文件夹中 Form_validation.php 中的 $_error_prefix 和 $_error_suffix。

标签: php codeigniter


【解决方案1】:

您可以通过这种方式更改错误分隔符

<ul>
<?php echo validation_errors('<li>', '</li>'); ?>
</ul>

文档:https://www.codeigniter.com/user_guide/libraries/form_validation.html#changing-the-error-delimiters

对于 v3:https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters

【讨论】:

    【解决方案2】:

    您可以通过创建 application/libraries/MY_form_validation.php 添加额外的验证规则来扩展 form_validation 类以获得最大控制 - 我在下面附上了一个示例。

    直接编辑系统库是不好的做法; CI 提供了更好的选择(通过MY_ 类、librarieshooks 等覆盖/自定义)。这使您可以轻松升级 CI 版本,并使您的应用程序可移植/自定义代码与核心框架隔离。

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    /**
     * CodeIgniter Form Validation Extension
     */
    class MY_Form_validation extends CI_Form_validation {
    
        /**
         *  MY_Form_validation::valid_url
         * @abstract Ensures a string is a valid URL
         */
        function valid_url($url) {
            if(preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) {
                if(filter_var($url, FILTER_VALIDATE_URL)) return TRUE;
            }
            $this->CI->form_validation->set_message('valid_url', 'The %s must be a valid URL.');
            return FALSE;
        }
    
        /**
         * MY_Form_validation::alpha_extra()
         * @abstract Alpha-numeric with periods, underscores, spaces and dashes
         */
        function alpha_extra($str) {
            $this->CI->form_validation->set_message('alpha_extra', 'The %s may only contain alpha-numeric characters, spaces, periods, underscores & dashes.');
            return ( ! preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
        }
    
        /**
         * MY_Form_validation::numeric_comma()
         * @abstract Numeric and commas characters
         */
        function numeric_comma($str) {
            $this->CI->form_validation->set_message('numeric_comma', 'The %s may only contain numeric & comma characters.');
            return ( ! preg_match("/^(\d+,)*\d+$/", $str)) ? FALSE : TRUE;
        }
    
        /**
         * MY_Form_validation::matches_pattern()
         * @abstract Ensures a string matches a basic pattern
         */
        function matches_pattern($str, $pattern) {
            if (preg_match('/^' . $pattern . '$/', $str)) return TRUE;
            $this->CI->form_validation->set_message('matches_pattern', 'The %s field does not match the required pattern.');
            return FALSE;
        }   
    
    }
    
    /* End of file MY_form_validation.php */
    /* Location: ./{APPLICATION}/libraries/MY_form_validation.php */
    

    【讨论】:

      【解决方案3】:

      你可以使用 &lt;?php echo form_error('field name', '&lt;div class="error"&gt;', '&lt;/div&gt;'); ?&gt; 用于单独显示错误。

      Documentation

      【讨论】:

        【解决方案4】:

        您可以使用 CodeIgniter 的 set_error_delimiters 函数将分隔符从 &lt;div&gt; 更改为 &lt;li&gt;

        $this->form_validation->set_error_delimiters('<li>', '</li>');
        

        您应该在加载表单验证类后立即执行此操作。

        这将改变validation_errors() form_error('field_name') 的显示方式。因此,您需要添加ulol,如下所示:

        echo '<ul>' . validation_errors() . '</ul>';
        

        【讨论】:

          【解决方案5】:

          与上面的答案相同,只是如果您想要使用引导程序:

          <ul>
          <?php echo validation_errors('<li><span class="label label-danger">', '</span></li>'); ?>
          </ul>
          

          【讨论】:

            【解决方案6】:

            3 种格式化/自定义错误显示的方法

            1.全局更改分隔符:要在控制器方法中全局更改错误分隔符,就在加载表单验证类之后。在 form_validation 库加载之后加载。你也可以在构造函数中加载。

            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            

            2.单独更改分隔符:本教程中显示的两个错误生成函数中的每一个都可以提供自己的分隔符,如下所示

            <?php echo form_error('field name', '<div class="error">', '</div>'); ?>
            OR
            <?php echo validation_errors('<div class="error">', '</div>'); ?>
            

            3.在配置文件中设置分隔符: 您可以在 application/config/form_validation.php 中添加错误分隔符,如下所示。在文件中设置这 2 个配置变量。 $config['error_prefix'] ='&lt;div class="error_prefix"&gt;';$config['error_suffix'] = '&lt;/div&gt;';

            参考链接:https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters

            【讨论】:

              【解决方案7】:

              看看system/language/english/form_validation_lang.php

              我相信你可以编辑它,或者复制到application/language/english/form_validation_lang.php

              【讨论】:

              • 我们可以将同一文件的副本复制到application/language/english/form_validation_lang.php(Applicatoin 的语言文件夹)并进行任何我们想要的自定义
              【解决方案8】:

              为了使用 javascript 验证页面中的 devexpress 控件,请使用以下代码:

              ASPxClientEdit.ValidateGroup(null);
              

              ASPxClientEdit.ValidateGroup('validationgroup');
              

              【讨论】:

                猜你喜欢
                • 2011-10-30
                • 1970-01-01
                • 2013-01-05
                • 2014-06-08
                • 2020-07-02
                • 2012-07-01
                • 1970-01-01
                • 2020-04-27
                • 2013-03-24
                相关资源
                最近更新 更多