在编码器中设置错误信息


Set Error Message in Codeigniter

我正在使用form_validation配置文件来设置验证规则,但是是否也可以在那里设置错误消息?我试着设置一个关联数组,但它不起作用。验证本身可以工作,但是它说:

"无法访问与您的字段名Username对应的错误消息。"

array(
        'field' => 'username',
        'label' => 'Username',
        'rules' => 'trim|required|alpha_numeric_dash_space|min_length[4]|max_length[25]|is_unique[User.username]',
        array(
            'required' => 'You have not provided a {field}',
            'is_unique' => 'This {param} already exists',
            'alpha_numeric_dash_spaces' => 
              'The {field} may only contain alphanumeric characters, underscores, dashs, and spaces'
        )
    )

还是需要手动设置控制器中的错误信息?

你得到这个消息的原因是因为CodeIgniter正在查找form_validation_lang.php,而不是在那里为你的规则匹配任何东西。

据我所知,你唯一的选择是在application > language > ** language ** > form_validation_lang.php创建一个新文件,并以下面的格式添加你的消息,CodeIgniter应该挑选它们。

我还没有测试过,但是,如果这不起作用-虽然我很确定它会-然后在system > language > ** language ** > form_validation_lang.php中添加额外的行,如:

$lang['alpha_numeric_dash_spaces'] = 'The {field} may only contain alphanumeric characters, underscores, dashs, and spaces';

或者在控制器中手动设置消息:

$this->form_validation->set_message('alpha_numeric_dash_spaces', 'The {field} may only contain alphanumeric characters, underscores, dashs, and spaces');