Archived
1
0
Fork 0

adding app/forms/Registration.php

This commit is contained in:
Henrik Hautakoski 2018-08-14 21:28:30 +02:00
parent 31d5740681
commit 6eb08d7a70
No known key found for this signature in database
GPG key ID: 839F3A7EAFAEAFAA

118
app/forms/Registration.php Normal file
View file

@ -0,0 +1,118 @@
<?php
namespace App\Form;
/**
* Models
*/
use App\Model\Data\User as UserModel;
/**
* Phalcon Form
*/
use App\Model\Data\User;
use Httpcb\Form as FormBase,
Phalcon\Forms\Element as FormElement;
/**
* Element types
*/
use Httpcb\OAuth\UserData\UserDataInterface;
use Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Submit;
/**
* Validators
*/
use Phalcon\Validation\Validator\Callback as CallbackValidator,
Phalcon\Validation\Validator\Uniqueness as UniquenessValidator,
Phalcon\Validation\Validator\Alnum as AlnumValidator,
Phalcon\Validation\Validator\Email as EmailValidator,
Phalcon\Validation\Validator\StringLength as StringLengthValidator,
Phalcon\Validation\Validator\Identical as IdenticalValidator,
Httpcb\Validation\Validator\Alpha as AlphaValidator;
class Registration extends FormBase
{
public function initialize()
{
$this->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);
}
}
}