2013-02-03 61 views
0

當我試圖設置自定義消息,使用下面的語句中的CodeIgniter下from_validation庫是錯誤的..笨set_message()

$this->form_validation->set_message("is_unique", "%s already registered. Try a different %s"); 

非但沒有Username already registered. Try a different Username的我越來越Username already registered. Try a different users.username

雖然文檔說If you include %s in your error string, it will be replaced with the "human" name you used for your field when you set your rules.

請讓我走出困境。

回答

-2

您可以設置自定義消息與codigniter的信息庫

對於實例 在型號編碼篩選

if(duplicate_user_name ==true){ 
    $this->messages->add('$user already registered. Try a different $user ', 'error'); 
} 

,並與下面的視圖

$messages = $this->messages->get(); 
      if (is_array($messages)): 
       foreach ($messages as $type => $msgs): 
        if (count($msgs > 0)): 
         foreach ($msgs as $message): 
          echo ('<div class="' . $type .'">' . $message . '</div>'); 
         endforeach; 
        endif; 
       endforeach; 
      endif; 
+0

這是最好的答案,我知道請參閱編輯答案欲瞭解更多信息 –

0

我面臨同樣的問題,而我尋找,我發現這個answer這笨提到的行構建錯誤消息(位於文件系統/ libraries/form_validation.php中)在673行附近:

// Build the error message 
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param); 

我把它編輯成:

// Build the error message 
if($rule == 'is_unique') {          //my code here 

    $vars_num = substr_count($line, '%s');      //my code here 
    for($i = 0; $i < $vars_num; $i++){       //my code here 
     $params[] = $this->_translate_fieldname($row['label']); //my code here 
    }               //my code here 
    $message = vsprintf($line, $params);      //my code here 

} else {              //my code here 

    $message = sprintf($line, $this->_translate_fieldname($row['label']), $param); 

} 

,它爲我工作得很好。 我知道編輯系統庫不是最佳做法,但它可能會對您有所幫助。

0

試試這個:

$this->form_validation->set_message("is_unique", "{field} already registered. Try a different {field}");