如何在codeIgniter中自定义表单验证错误

时间:2012-06-30 04:59:45

标签: php codeigniter

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

enter image description here

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

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

<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()函数的数据,它从表单输出验证错误。

8 个答案:

答案 0 :(得分:11)

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

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

docs: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

答案 1 :(得分:8)

您可以通过创建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 */

答案 2 :(得分:3)

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

Documentation

答案 3 :(得分:2)

您可以使用CodeIgniter的<div>函数将分隔符从<li>更改为set_error_delimiters

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

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

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

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

答案 4 :(得分:1)

与上面的答案相同,就像你想要使用bootstrap:

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

答案 5 :(得分:1)

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'] ='<div class="error_prefix">';$config['error_suffix'] = '</div>';

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

答案 6 :(得分:0)

查看system/language/english/form_validation_lang.php

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

答案 7 :(得分:-8)

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

ASPxClientEdit.ValidateGroup(null);

ASPxClientEdit.ValidateGroup('validationgroup');
相关问题