2014-02-24 94 views
1

所以我試圖添加自定義驗證規則是這樣的:Laravel 4定製驗證錯誤消息

public function validateDateAfterField($attribute, $value, $params) 
{ 
    return strtotime($value) > strtotime($this->data[$params[0]]); 
} 

它需要的值,並與另一場驗證在數據被的陣列驗證

我想它產生的錯誤信息是這樣的:

Event End Date must be a date after Event Start Date 

這裏是我的自定義驗證消息:

":attribute must be a date after :before" 

我已經添加了這些自定義屬性驗證:

'attributes' => array(
     'event_start'=>'Event Start Date', 
     'event_end' => 'Event End Date', 
    ) 

但是,當我不能在我的自定義驗證類訪問它,我已經試過這樣的事情:

return str_ireplace(":before", $this->attributes[$parameters[0]], $message); 

,但得到一個錯誤Undefined property:CustomerValidator::attributes

任何人都知道如何正確地做到這一點?

編輯:

擴展它在我的global.php

Validator::extend('date_after_field', '[email protected]'); 
Validator::resolver(function($translator, $data, $rules, $messages) 
{ 
    return new CustomValidator($translator, $data, $rules, $messages); 
}); 
+0

您是否在此驗證中定義了一個Validator :: extend方法? – revo

+0

是的,我已經在我的global.php中定義了它,我將添加代碼 –

回答

3

我已經找到了答案讀取驗證類的文檔後。

通過使用$this->getAttribute($parameters[0]),我設法得到自定義屬性名稱。