如何在 Codeigniter 上设置自定义错误消息

How do I Set custom error messages on Codeigniter

我正在尝试根据 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.

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

请尝试以下步骤:

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

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