<?php
class User extends \Phalcon\Mvc\Model
{
protected $id;
protected $name;
protected $email;
protected $mobile;
protected $password;
public function initialize()
{
$this->useDynamicUpdate(true);
}
public function validation()
{
$this->validate(new Email(array(
"field" => "email",
"message" => "The email is invalid"
)));
$this->validate(new Uniqueness(array(
"field" => "email",
"message" => "The email must be unique"
)));
return $this->validationHasFailed() != true;
}
...................
}
$record = User::findFirst(1);
$record->setMobile('13507083599');
if ($record->update() == false) {
foreach ($record->getMessages() as $message) {
echo $message, "\n";
}
} else {
echo "update ok";
}
when I only update the mobile field, the email field validation is also excecuted, any solution for validating dynamic update fields?
I try to move the validation to the setter method, but the result is not right.
public function setEmail($email)
{
$this->validate(new Email(array(
"field" => "email",
"message" => "The email is invalid"
)));
$this->validate(new Uniqueness(array(
"field" => "email",
"message" => "The email must be unique"
)));
if ($this->validationHasFailed()) {
foreach ($this->getMessages() as $message) {
throw new \Exception($message);
}
return false;
}
$this->email = $email;
return $this;
}