118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|