How to customize form validation errors in codeIgniter

前端 未结 8 2230
刺人心
刺人心 2021-01-05 07:18

Is there a file in codeIgniter in which I could just edit so that I can customize the form validation messages?

<script

相关标签:
8条回答
  • 2021-01-05 07:36

    3 Ways to format/ customize error display

    1. Changing delimiters Globally: To globally change the error delimiters, in your controller method, just after loading the Form Validation class. Load after the form_validation library load. you can load in the constructor as well.

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

    2.Changing delimiters Individually: Each of the two error generating functions shown in this tutorial can be supplied their own delimiters as follows

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

    3. Set delimiters in a config file: You can add your error delimiters in application/config/form_validation.php as follow. Set this 2 config variable in the file. $config['error_prefix'] ='<div class="error_prefix">'; and $config['error_suffix'] = '</div>';

    Ref Link: https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters

    0 讨论(0)
  • 2021-01-05 07:41

    You can change the delimiters from <div> to <li> by using CodeIgniter's set_error_delimiters function:

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

    You should do this immediately after loading the Form Validation class.

    This will change the way validation_errors() and form_error('field_name') are displayed. So you will need to add ul or ol as follows:

    echo '<ul>' . validation_errors() . '</ul>';
    
    0 讨论(0)
  • 2021-01-05 07:42

    The same as answers above, just if you want with bootstrap:

    <ul>
    <?php echo validation_errors('<li><span class="label label-danger">', '</span></li>'); ?>
    </ul>
    
    0 讨论(0)
  • 2021-01-05 07:45

    In order to validate devexpress controls in the page using javascript, use the following code:

    ASPxClientEdit.ValidateGroup(null);
    

    or

    ASPxClientEdit.ValidateGroup('validationgroup');
    
    0 讨论(0)
  • 2021-01-05 07:51

    You can extend the form_validation class for maximum control by creating application/libraries/MY_form_validation.php to add extra validation rules - I've attached an example below.

    It is bad practice to edit system libraries directly; CI provides better options (overrides/customization thru MY_ classes, libraries, hooks, etc). This gives you the benefit of easily upgrading CI versions & keeps your application portable / custom code isolated from the core framework.

    <?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 */
    
    0 讨论(0)
  • 2021-01-05 07:52

    Take a look at system/language/english/form_validation_lang.php

    I believe you can either edit it, or copy it to application/language/english/form_validation_lang.php

    0 讨论(0)
提交回复
热议问题