CodeIgniter: set_message for max_length [x]

How do you set an error message for max_lengthand rules min_length. For example, if I set a rule max_length[6], I would like the error message to be displayed Max characters allowed: 5

+3
source share
6 answers

gAMBOOKa,

create a new rule that also checks max_length

$this->form_validation->set_rules('username', 'Username', 'required|_max_length[12]');

and for the method.

function _max_length($val)
    {
        if (strlen($this->input->post('username')) > $val)
        {
                    $this->form_validation->set_message('_max_length', 'Max characters allowed: 5')
            return FALSE;
        }

        return TRUE;
    }

add this to your controller as a new rule and set the corresponding message --- ^

0
source

, , . % s . (http://codeigniter.com/user_guide/libraries/form_validation.html#settingerrors) , :

$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');

% s , "max_length", , :

$this->form_validation->set_message('max_length', 'The field %s max length is %s');

. , . , .

+3

application/language/en/en_lang.php :

$lang['name'] = "Name";
$lang['form_required'] = "is required.";

application/language/es/es_lang.php :

$lang['name'] = "Nombre";
$lang['form_required'] = "es requiero.";

application/controllers/yourController.php :

$this->form_validation->set_rules('name', $this->lang->line('name'), 'required|alpha|xss_clean');

$this->form_validation->set_message('required', '%s ' . $this->lang->line('form_required'));

, !

+3

@ . Per CodeIgniter, (, "min_length", "max_length" ..), :

$this->form_validation->set_message('validation_rule', 'Your message here');

, :

$this->form_validation->set_message('max_length', 'Max characters allowed: 5');

, .

+1

system/language/english/form_validation_lang.php,

$lang['max_length'] = "The %s field can not exceed %s characters in length.";

,

application/language/english/form_validation_lang.php

And changing it to the desired line. Do not edit the file in system/directly, then it will be overwritten if you update CodeIgniter.

0
source

Source: https://habr.com/ru/post/1722274/


All Articles