How do you customize/style codeigniter errors?

后端 未结 5 854
野性不改
野性不改 2021-01-04 19:17

I\'m trying to customize the CSS/HTML for error message displays in codeigniter so I can apply a tag too each and style them up.

I tried to Google this and search t

相关标签:
5条回答
  • 2021-01-04 19:58

    You can do something like this...

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

    See the related Codeigniter documentation

    Note: Updated to correct function reference (validation should be form_validation).

    0 讨论(0)
  • 2021-01-04 19:58
    $this->validation->set_error_delimiters('<div class="error">', '</div>');
    

    This is wrong syntax. the Correct is

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
    
    0 讨论(0)
  • 2021-01-04 20:03

    I recommend more elegant way.

    Сreated a MY_Form_validation.php file and dropped it into application/libraries with the following code overriding the default delimiters.

    class MY_Form_validation extends CI_Form_validation {
    
        public function __construct()
        {
            parent::__construct();
    
            $this->_error_prefix = '<p class="error">';
            $this->_error_suffix = '</p>';
        }
    }
    

    Link to original: http://chris-schmitz.com/changing-default-error-delimiters-in-codeigniter/

    0 讨论(0)
  • 2021-01-04 20:13

    You can use the method "set_error_delimiters" of library "Form Validation":

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

    Also you can made on code inline with form helper:

    validation_errors('<div class="error">', '</div>')
    

    Or created extends class on form_validation library:

    class MY_Form_validation extends CI_Form_validation
    {
        public function __construct( $rules = array() )
        {
            // applies delimiters set in config file.
            if( ! isset( $rules['error_prefix'] ) )
            {
                $rules['error_prefix'] = '<div class="error">';
            }
    
            if( ! isset( $rules['error_suffix'] ) )
            {
                $rules['error_suffix'] = '</div>';
            }
    
            parent::__construct( $rules );
        }
    }
    

    I like the last method because allow set a style by default and overwrite from before method explain.

    Sorry for my english :)

    0 讨论(0)
  • 2021-01-04 20:14
    $this->validation->set_error_delimiters('<div class="error">', '</div>');
    
    0 讨论(0)
提交回复
热议问题