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); } /**