我正在嘗試根據codeigniter 的文檔為某些特定規則的特定字段設置自定義錯誤消息,但我遇到了問題。我的代碼如下所示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_length或max length。我已經完全按照文檔進行操作,但我不明白為什么會出現此問題。我知道有一種方法可以使用回調函數拋出自定義消息,但我最好使用這種方法。
1 回答

POPMUISE
TA貢獻1765條經驗 獲得超5個贊
請嘗試以下步驟:
$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。
我希望其中之一會有所幫助!
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消