2014-09-22 50 views
0

我在我的Laravel項目中使用了Ardent,因爲我喜歡它的超棒模型驗證功能。現在我需要在驗證錯誤消息中設置可理解的屬性值。 我讀過它可以通過在validation.php文件(lang文件夾)中設置$ attributes數組來完成。Ardent驗證消息中的自定義屬性名稱

因爲我是來自Yii,我想知道是否有一種方法可以在模型基礎上指定屬性名稱,而不是全局在validation.php lang文件中。

例如,如果我有兩個模型,Employee和Company,都帶有「name」屬性,我想分別將「name」屬性的顯示值設置爲「Employee name」和「Company name」。

回答

0

試試看。它來自GitHub上的文檔。我從來沒有和Ardent合作過,但實際上它有很好的寫法。

class Employee extends \LaravelBook\Ardent\Ardent { 
    public static $rules = array(
     'name' => 'Required|Alpha_Dash' 
    ); 
    public static $customMessages = array(
     'required' => 'The Employee :attribute field is required.', 
     'alpha_dash' => 'The Employee :attribute field must be Letters and Dashes only.' 
    ); 
} 

由於這些自定義消息在僱員類中定義,它們只從這個類適用於輸入。你可以做同樣的公司類:

class Company extends \LaravelBook\Ardent\Ardent { 
    public static $rules = array(
     'name' => 'Required|Alpha_Dash' 
    ); 
    public static $customMessages = array(
     'required' => 'The Company :attribute field is required.', 
     'alpha_dash' => 'The Company :attribute field must be Letters and Dashes only.' 
    ); 
} 

我認爲這應該工作。但是我沒有任何方法來測試它。另一種選擇是使用自定義規則,如

'name' => 'Employee_Required|Employee_Aplha_Dash' 

並定義那些在Laravel的驗證。但無論如何,我希望這有助於!

+0

這只是一種解決方法。也許Ardent不支持這個功能。 – 2014-09-22 20:15:51

+0

我已經成功地研究了Ardent,除了上面的方法之外,我看不到一種方法來實現所需的功能(即不修改Ardent的原始源代碼) – 2014-09-23 13:07:02