2011-08-31 56 views
2

我有一個模型,我想要翻譯表單驗證錯誤。讓我感動的$validate填充到構造:通過帶構造函數的模型調用成員函數trigger()

function __construct() { 
    $this->validate = array(
     'url' => array(
      'url' => array(
       'rule' => array('url'), 
       'message' => __('Enter a valid URL', true), 
      ), 
     ), 
     'revisit' => array(
      'numeric' => array(
       'rule' => array('numeric'), 
       'allowEmpty' => true, 
      ), 
     ), 
     'reading_list' => array(
      'boolean' => array(
       'rule' => array('boolean'), 
      ), 
     ), 
    ); 
} 

每當我打開任何網頁,我得到以下錯誤:

Fatal error: Call to a member function trigger() on a non-object in /home/mu/Branches/cakemarks/cake/libs/model/model.php on line 2106 

Call Stack: 
    0.0005  347980 1. {main}() /home/mu/Branches/cakemarks/app/webroot/index.php:0 
    0.0548 3580716 2. Dispatcher->dispatch() /home/mu/Branches/cakemarks/app/webroot/index.php:83 
    0.0597 3735300 3. Dispatcher->_invoke() /home/mu/Branches/cakemarks/cake/dispatcher.php:171 
    0.1451 7428100 4. call_user_func_array() /home/mu/Branches/cakemarks/cake/dispatcher.php:204 
    0.1451 7428336 5. BookmarksController->startscreen() /home/mu/Branches/cakemarks/cake/dispatcher.php:0 
    0.1451 7429132 6. Model->find() /home/mu/Branches/cakemarks/app/controllers/bookmarks_controller.php:100 

之前我曾經在那裏__()功能,我可以有$validate外面的領域,它運作良好。

我該如何在這裏驗證消息呢?

回答

3

重寫構造函數是好的,但你需要確保原來的構造函數運行:

public function __construct($id = false, $table = null, $ds = null) { 
    // do your thing 

    parent::__construct($id, $table, $ds); 
} 

此外,the manual recommends

If you would like to have all of your validation error messages translated by default, a simple solution would be to add the following code in you app_model.php :

function invalidate($field, $value = true) { 
    return parent::invalidate($field, __($value, true)); 
}