【问题标题】:Call to a member function trigger() by a model with constructor具有构造函数的模型调用成员函数 trigger()
【发布时间】:2011-11-07 11:52:54
【问题描述】:

我有一个模型,我想在其中翻译表单验证错误。所以我把$validate的填充移到了构造函数中:

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,它运行良好。

我怎样才能在那里输入验证消息?

【问题讨论】:

    标签: php cakephp internationalization


    【解决方案1】:

    重写构造函数没问题,但是你需要确保原始构造函数运行:

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

    另外,the manual recommends:

    如果您希望默认翻译所有验证错误消息,一个简单的解决方案是在您的app_model.php 中添加以下代码:

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

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 2011-12-07
      • 2017-09-07
      • 2012-09-28
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多