【问题标题】:Set Error Message in Codeigniter在 Codeigniter 中设置错误消息
【发布时间】:2015-07-06 17:20:07
【问题描述】:

我正在使用form_validation 配置文件来设置验证规则,但是否也可以在其中设置错误消息?我尝试设置一个关联数组,但没有奏效。验证本身有效,但它说:

“无法访问与您的字段名称用户名对应的错误消息。”

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

或者我必须在控制器中手动设置错误消息?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您收到该消息的原因是因为 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');
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      相关资源
      最近更新 更多