开发者

Call to a member function trigger() by a model with constructor

开发者 https://www.devze.com 2023-04-01 17:39 出处:网络
I have a model where I want to translate the form validation error. So I moved the filling of the $validate into the constructor:

I have a model where I want to translate the form validation error. So I moved the filling of the $validate into the constructor:

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'),
            ),
        ),
    );
}

Whenever I open any page I get the following error:

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/开发者_StackOverflow社区cakemarks/app/controllers/bookmarks_controller.php:100

Before I used the __() function in there, I could have the $validate outside in the fields and it worked good.

How can I i18n the validation message there?


Overriding the constructor is fine, but then you need to make sure the original constructor runs:

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

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

Also, 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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号