【问题标题】:Kohana 3: Callback validationKohana 3:回调验证
【发布时间】:2010-12-15 12:35:05
【问题描述】:

注意:此问题仅针对 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 时我如何发送验证消息?

【问题讨论】:

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


    【解决方案1】:

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

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

    (从问题中移出)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多