diff --git a/app/forms/UserSettings.php b/app/forms/UserSettings.php index bd6d4d0..f51dab2 100644 --- a/app/forms/UserSettings.php +++ b/app/forms/UserSettings.php @@ -50,19 +50,24 @@ class UserSettings extends FormBase public function initialize() { + $entity = $this->getEntity(); + $this->setValidation(new \Phalcon\Validation()); // Id - $id = new Text('id', array( - 'class' => 'form-control', - 'readonly' => '', - )); - $id->addValidator(new IdenticalValidator([ - 'accepted' => $this->getEntity()->getId(), - ])); + if ($entity && $entity->getId()) { + $id = new Text('id', array( + 'class' => 'form-control', + 'readonly' => '', + )); - $id->setLabel('ID'); - $this->add($id); + $id->addValidator(new IdenticalValidator([ + 'accepted' => $entity->getId(), + ])); + + $id->setLabel('ID'); + $this->add($id); + } // Username $username = new Text('username', array( @@ -72,16 +77,20 @@ class UserSettings extends FormBase $username->setLabel('Username'); - $username->addValidator(new AlnumValidator()); - - $validator = new UniquenessValidator(array( + $validator_options = array( 'model' => new UserModel(), - 'message' => 'The username already exists.', + 'message' => 'The :field already exists.', 'attribute' => 'username', - 'except' => [ $this->getEntity()->getUsername() ] - )); + ); - $username->addValidator($validator); + if ($entity && strlen($entity->getUsername())) { + $validator_options['except'] = [ $entity->getUsername() ]; + } + + $username->addValidators([ + new AlnumValidator(), + new UniquenessValidator($validator_options) + ]); $this->add($username); @@ -100,18 +109,39 @@ class UserSettings extends FormBase $this->add($name); // Email - $email = new Text('email', array( - 'class' => 'form-control', - 'placeholder' => 'Email', - 'readonly' => '', - )); + if ($this->_admin === false && $entity) { + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + 'readonly' => '', + )); - $email->addValidator(new IdenticalValidator([ - 'accepted' => $this->getEntity()->getEmail(), - ])); + $email->addValidator(new IdenticalValidator([ + 'accepted' => $entity->getEmail(), + ])); + } else { + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + )); + + $validator_options = [ + 'model' => new UserModel(), + 'message' => 'The :field already exists.', + 'attribute' => 'email', + ]; + + if ($entity && strlen($entity->getEmail())) { + $validator_options['except'] = [ $entity->getEmail() ]; + } + + $email->addValidators([ + new EmailValidator(), + new UniquenessValidator($validator_options) + ]); + } $email->setLabel('Email'); - $this->add($email); // Passwords @@ -127,10 +157,10 @@ class UserSettings extends FormBase */ protected function _passwords() { - $current_pw = $this->getEntity()->getPassword(); + $entity = $this->getEntity(); // Current - if ($this->_admin === false && strlen($current_pw) > 0) { + if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) { $current = new Password('passwordCurrent', array( 'class' => 'form-control', )); @@ -155,7 +185,7 @@ class UserSettings extends FormBase // Validation $validation = $this->getValidation(); - if ($this->_admin === false && strlen($current_pw) > 0) { + if ($this->_admin === false && $entity && strlen($entity->getPassword()) > 0) { $validation->add('passwordCurrent', new CallbackValidator([ 'callback' => function($data) { $new_pw = $data['passwordNew'];