开发者

Kohana 3: Callback validation

开发者 https://www.devze.com 2023-01-31 05:36 出处:网络
NOTE: This question refers to Kohana 3.0 only.Kohana 3.1 and newer handle validation callbacks in a completely different way.

NOTE: This question refers to Kohana 3.0 only. Kohana 3.1 and newer handle validation callbacks in a completely different way.

I'm doing a validation with a callback (ORM). These are my code:

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)-开发者_Python百科>find_all();
        if(count($resultado)) {
            return false;
        }
        else {
            return true;
        }
    }
    // more code goes here!

It's returning true or false (if there is a value, returns false) but how could i send a validation message when it returns false?


The following validation function sets an error for the field if validation fails:

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

(Moved from question)

0

精彩评论

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