From dcff1a81b221e1710b739aea3cb0f612f43d0768 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Aug 2018 06:06:56 +0200 Subject: [PATCH 1/3] app/models/Data/Base.php: override hasChanged() Phalcon throws an exception if no valid snapshot data is present. We override this behaviour by treating no snapshot data (new row) as changed. --- app/models/Data/Base.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/models/Data/Base.php b/app/models/Data/Base.php index 2223d4b..59a92be 100644 --- a/app/models/Data/Base.php +++ b/app/models/Data/Base.php @@ -6,4 +6,21 @@ use Phalcon\Mvc\Model; class Base extends Model { + /** + * {@inheritdoc} + * + * Phalcon throws an exception if no valid snapshot data is present. + * + * We override this behaviour by treating no + * snapshot data (new row) as changed. + * + * @param string|array $fieldName + * @param boolean $allFields + * @return bool + */ + public function hasChanged($fieldName = null, $allFields = false) + { + return $this->hasSnapshotData() === false + || parent::hasChanged($fieldName, $allFields); + } } From bd51dd87004e543efabf07b7985ce46020b58887 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Aug 2018 06:08:02 +0200 Subject: [PATCH 2/3] app/models/Data/User.php: extend our base model instead of Phalcon\Mvc\Model directly. --- app/models/Data/User.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/Data/User.php b/app/models/Data/User.php index d813e8b..8c67250 100644 --- a/app/models/Data/User.php +++ b/app/models/Data/User.php @@ -2,13 +2,12 @@ namespace App\Model\Data; -use Phalcon\Mvc\Model, - Phalcon\Validation, +use Phalcon\Validation, Phalcon\Validation\Validator\Callback as CallbackValidator, InvalidArgumentException, Httpcb\OAuth\UserData\UserDataInterface; -class User extends Model +class User extends Base { const STATUS_ACTIVE = 'Active'; const STATUS_DELETED = 'Deleted'; From 28e2201a6b31141cd066e033aebb38460cb6abb2 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Aug 2018 06:08:52 +0200 Subject: [PATCH 3/3] app/models/Data/User.php: in validation() only validate changed fields. --- app/models/Data/User.php | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/app/models/Data/User.php b/app/models/Data/User.php index 8c67250..335a874 100644 --- a/app/models/Data/User.php +++ b/app/models/Data/User.php @@ -48,19 +48,31 @@ class User extends Base public function validation() { - // Validation - $validator = new Validation(); + $rules = [ + 'username' => new CallbackValidator([ + 'callback' => function () { + return $this->findFirstByUsername($this->getUsername()) === false; + }, + 'message' => 'The username already exists.' + ]), + 'email' => new CallbackValidator([ + 'callback' => function() { + return $this->findFirstByEmail($this->getEmail()) === false; + }, + 'message' => 'The email address already exists.' + ]) + ]; - $validator->add('username', new CallbackValidator([ - 'callback' => function() { return $this->findFirstByUsername($this->getUsername()) === false; }, - 'message' => 'The username already exists.' - ])); - $validator->add('email', new CallbackValidator([ - 'callback' => function() { return $this->findFirstByEmail($this->getEmail()) === false; }, - 'message' => 'The email address already exists.' - ])); + $validation = new Validation(); + foreach($rules as $field => $validator) { - return $this->validate($validator); + // Only validate changed fields. + if ($this->hasChanged($field)) { + $validation->add($field, $validator); + } + } + + return $this->validate($validation); } /**