From 6eb08d7a7030e0ec32731d2813499d3cb10bcb55 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 14 Aug 2018 21:28:30 +0200 Subject: [PATCH] adding app/forms/Registration.php --- app/forms/Registration.php | 118 +++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 app/forms/Registration.php diff --git a/app/forms/Registration.php b/app/forms/Registration.php new file mode 100644 index 0000000..c95adff --- /dev/null +++ b/app/forms/Registration.php @@ -0,0 +1,118 @@ +setValidation(new \Phalcon\Validation()); + + // Username + $username = new Text('username', array( + 'class' => 'form-control', + 'placeholder' => 'Username', + )); + + $username->setLabel('Username'); + + $username->addValidator(new AlnumValidator()); + + $validator = new UniquenessValidator(array( + 'model' => new UserModel(), + 'message' => 'The username already exists.', + 'attribute' => 'username', + )); + + $username->addValidator($validator); + + $this->add($username); + + // Names + foreach([ 'first-name' => 'Firstname', 'last-name' => 'Lastname' ] as $id => $label) { + + $name = new Text($id, array( + 'class' => 'form-control', + 'placeholder' => $label, + )); + + $name->setLabel($label); + $name->addValidator(new AlphaValidator([ + 'allowSpace' => false, + 'allowEmpty' => true, + ])); + + $this->add($name); + } + + // Email + $email = new Text('email', array( + 'class' => 'form-control', + 'placeholder' => 'Email', + 'readonly' => '', + )); + + $email->addValidators([ + new IdenticalValidator([ + 'accepted' => $this->getEntity()->getEmail(), + ]), + new UniquenessValidator([ + 'model' => new UserModel(), + 'message' => 'This email already exist.', + 'attribute' => 'email', + ]) + ]); + + $email->setLabel('Email'); + $this->add($email); + + // Submit + $submit = new Submit('submit', array('class' => 'button button-success', 'value' => 'Register')); + $this->add($submit); + } + + public function bind(array $data, $entity, $whitelist = null) + { + parent::bind($data, $entity, $whitelist); + + if ($entity instanceof User && $this->getEntity() instanceof UserDataInterface) { + $provider = $this->getEntity()->getProvider(); + $id = $this->getEntity()->getId(); + $entity->setOAuthId($provider, $id); + } + } +}