【问题标题】:How do I Set custom error messages on Codeigniter如何在 Codeigniter 上设置自定义错误消息
【发布时间】:2023-04-03 13:28:02
【问题描述】:

我正在尝试根据codeigniter's doc 在某些特定规则上为特定字段设置自定义错误消息,但我遇到了问题。我的代码如下所示

function start(){
$this->form_validation->set_message('rule1', 'Rule 1 Error Message');

$this->form_validation->set_rules('amount', lang('users amount'), 'required|trim|numeric|greater_than[0]', array('rule1' => 'Error Message on rule1 for this field_name'));

if ($this->form_validation->run() == FALSE)
{
  $this->index();
}
else{
$amount =$this->input->post("amount", TRUE);
}

我已经测试了每个字段的规则并且它们工作正常,当我为每个表单字段添加自定义错误消息时问题就开始了。以前,如果出现错误,我会重定向并显示整个表单的通用消息。

$this->session->set_flashdata('error', "Please provide correct data about the transfer");

使用自定义消息,当我输入不遵守规则的数据时,它根本不会抛出自定义消息,但是对于像min_lengthmax length 这样的预定义规则,它确实会抛出错误。

我已经按照文档进行了操作,但我不明白为什么会出现这个问题。我知道有一种方法可以使用回调函数抛出自定义消息,但最好我想使用这种方法。

【问题讨论】:

    标签: php forms codeigniter validation codeigniter-form-validation


    【解决方案1】:

    请尝试以下步骤:

    $this->session->set_flashdata('field_name', "Any custom error message");
    redirect('page_URL');//use url to be redirected upon.
    

    $this->form_validation->set_message('is_unique', 'The %s is already taken');
    

    form_validation 使用为字段设置的标签更改 %s。

    我希望其中之一会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      相关资源
      最近更新 更多