It seems that notNullValidations => false
is not working on model->save()
.
Detail is below.
class ModelBase extends \Phalcon\Mvc\Model
{
public function initialize()
{
\Phalcon\Mvc\Model::setup([
'notNullValidations' => false
]);
}
}
use Phalcon\Validation\Validator\Email as EmailValidator;
class User extends ModelBase
{
public function handleSaveError()
{
$this->handleError('save');
}
public function handleError($type = 'save')
{
if ($this->getMessages()) {
$messages = '';
foreach ($this->getMessages() as $message) {
$msg = sprintf('(field: %s, message: %s)', $message->getField(), $message->getMessage());
$messages .= ' ' . $msg . "\n";
}
throw new \Exception("Model $type error:" . "\n" . $messages);
}
}
public function validation()
{
$validator = new Validation();
$validator->rules('email', [
new EmailValidator(array(
)),
]);
return $this->validate($validator);
}
}
$user = User::findFirstById($userId);
$user->setEmail(null);
$user->save() or $user->handleSaveError();
then I got
2016-08-30 18:57:04 [ERROR] Model save error:
(field: email, message: Field email must be an email address)
How do I solve this??
Anyone help!