Kohana 3:回调验证

时间:2010-12-15 12:35:05

标签: validation callback kohana-3 kohana-orm kohana-3.0

  

注意:此问题仅适用于 Kohana 3.0 。 Kohana 3.1和更新的处理验证回调以完全不同的方式。

我正在使用回调(ORM)进行验证。这些是我的代码:

class Model_Loja extends ORM {
    // more code goes here!
    protected $_callbacks = array(
        'endereco' => array('endereco_unico')
    );

    public function endereco_unico(Validate $validate, $campo) {
        $resultado = $this->where('endereco', '=', $this->endereco)->find_all();
        if(count($resultado)) {
            return false;
        }
        else {
            return true;
        }
    }
    // more code goes here!

它返回true或false(如果有值,返回false)但是如何在返回false时发送验证消息?

1 个答案:

答案 0 :(得分:0)

如果验证失败,以下验证函数会为字段设置错误:

public function endereco_unico(Validate $validate, $campo) {
    if(count($this->where('endereco', '=', $this->endereco)->find_all())) {
        $validate->error($campo, 'endereco_unico');
    }
}

(移出问题)