How to configure form validation errors in codeIgniter

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

enter image description here

I just want to put them on a bulleted list to consume less space.

Here is the code I use to display error messages:

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

Basically, it $alert['sub_message']just gets its data from a function validation_errors()from CodeIgniter that prints validation errors from the form.

+5
source share
7 answers

You can extend the form_validation class for maximum control by creating application/libraries/MY_form_validation.phpadditional validation rules to add. I have added an example below.

; CI ( / MY_, libraries, hooks, .). 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 */
+8

:

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

docs: http://ellislab.com/codeigniter/user-guide/librarie/form_validation.html#errordelimiters

+10

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

+2

<div> <li> CodeIgniter set_error_delimiters:

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

.

validation_errors() form_error('field_name'). ul ol :

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

, , , :

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

///form_validation_lang.php

, //english/form_validation_lang.php

0

devexpress javascript, :

ASPxClientEdit.ValidateGroup(null);

ASPxClientEdit.ValidateGroup('validationgroup');
-8

All Articles