How to call all validation (built in and user validation)

If i call $model->validation(), it only call user specified validation. The automatic built in validation from the schema, like presenceOf is not called. I want to call all the validation (user & builr in) before saving/update. pls help

Try parent::validate() in $model->validation()

parent::validate() not work either :(



368

you need to create that.

public function getMessages() { //reset default $this->_errorMessages = array();

$this->validation();

return $this->_errorMessages;

}

/** * Validation * @return bool */ public function validation() { $this->validate(new PresenceOf(array( 'field' => 'username', 'message' => 'Polje korisničko ime je obvezno.' )));

$this->validate(new StringLength(array(
     'field' => 'username',
     'max' => 20,
     'min' => 6,
     'message' => 'Polje korisničko ime je obvezno.',
     'messageMaximum' => 'Korisničko ime može sadržati najviše 20 znakova.',
     'messageMinimum' => 'Korisničko ime mora sadržati najmanje 6 znakova.'
)));


$this->validate(new PresenceOf(array(
    'field' => 'password',
    'message' => 'Polje lozinka je obvezno.'
)));


$this->validate(new StringLength(array(
    'field' => 'password',
    'max' => 20,
    'min' => 6,
    'messageMaximum' => 'Lozinka može sadržati najviše 20 znakova.',
    'messageMinimum' => 'Lozinka mora sadržati najmanje 6 znakova.'
)));


$this->validate(new PresenceOf(array(
    'field' => 'e_mail',
    'message' => 'Polje email je obvezno.'
)));


return $this->validationHasFailed() != true;

}



368

And you call $model->getMessages();

Yes, but built in validation has also a foreign key contraint, InvalidCreateAttempt, InvalidUpdateAttempt, ConstraintViolation, that it is very handy if I have not to rewrite that validation.



368

If you want to use default validations just call then $model->getMessages();

@Igor, I confirm to you that overriding getMessages by resetting $this->_errorMessages = array(); and call validation did not call the default validation. I've tried that.



13.3k

Stackoverflow is a better place for asking question and here is better for discussion.



84.5k

There is no way to abstract the built-in validation outside of a save. You can use the validation component to abstract the validation and re-use it outside the model and inside it.

@Phalcon, how about this workaround, it can work right?

class ModelBase extends \Phalcon\MVC\Model{
    protected $_fakeSave = false;

    protected function afterValidation(){
        return !_fakeSave;
    }

    public function valid(){
        $this->_fakeSave = true;
        $result = $this->save();
        $this->_fakeSave = false;        
        return $this->getMessages();
    }
}