开发者

How do you customize/style codeigniter errors?

开发者 https://www.devze.com 2023-01-12 08:07 出处:网络
I\'m trying to customize the CSS/HTML for error message displays in codeigniter so I can apply atag t开发者_运维知识库oo each and style them up.

I'm trying to customize the CSS/HTML for error message displays in codeigniter so I can apply a tag t开发者_运维知识库oo each and style them up.

I tried to Google this and search the manual but must have been searching for the wrong terms - can anyone help me out?


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).


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/


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


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


$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

精彩评论

暂无评论...
验证码 换一张
取 消