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
|
Apr '13 |
6 |
749 |
1 |
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
you need to create that.
public function getMessages() { //reset default $this->_errorMessages = array();
$this->validation();
return $this->_errorMessages;
}
/**
@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; }
@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.
@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();
}
}