Here's my code:
registerAction() in controllers/SessionController.php
public function registerAction() {
$form = new RegisterForm();
if ($this->request->isPost()) {
if ($this->security->checkToken()) {
if ($form->isValid($_POST)) {
$user = new Users;
$user->username = $this->request->getPost('username');
$user->email = $this->request->getPost('email');
$user->password = $this->security->hash($this->request->getPost('password'));
$user->created_at = time();
$user->create();
}
else
{
echo $form->getMessages()[0];
}
}
}
$this->view->form = $form;
}
models/User.php Model:
use Phalcon\Mvc\Model\Validator\Uniqueness;
class Users extends Phalcon\Mvc\Model
{
public function validation()
{
$this->validate(new Uniqueness(array(
'field' => 'email',
"message" => "Value of field 'email' is already present in another record"
)));
$this->validate(new Uniqueness(array(
'field' => 'username',
"message" => "Value of field 'username' is already present in another record"
)));
if ($this->validationHasFailed() == true) {
return false;
}
}
public function initialize() {
$this->skipAttributesOnCreate(array('status'));
}
}
views/session/register.phtml
<?=$this->getContent();?>
<?php use Phalcon\Tag; ?>
<h1 class="main_heading">Create a new Account!</h1>
<div class="form">
<form action="register" method="post">
<p>
<label for="username"><?=$form->getLabel('username');?></label>
<?=$form->render('username');?>
</p>
<p>
<label for="email"><?=$form->getLabel('email');?></label>
<?=$form->render('email');?>
</p>
<p>
<label for="email"><?=$form->getLabel('password');?></label>
<?=$form->render('password');?>
</p>
<p>
<input type="submit" value="Register" name="submit">
</p>
<input type="hidden" name="<?php echo $this->security->getTokenKey() ?>" value="<?php echo $this->security->getToken() ?>"/>
</form>
</div>
forms/RegisterForm.php
use Phalcon\Forms\Form,
Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Submit,
Phalcon\Forms\Element\Check,
Phalcon\Forms\Element\Hidden,
Phalcon\Validation\Validator\Regex,
Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\StringLength,
Phalcon\Validation\Validator\Email,
Phalcon\Validation\Validator\Identical,
Phalcon\Validation\Validator\Uniqueness;
class RegisterForm extends Form
{
public function initialize()
{
/**
* Validate Username
*/
$username = new Text('username', array(
'placeholder' => 'Username'
));
$username->setLabel('Username');
$username->addValidators(array(
new PresenceOf(array(
'message' => 'Please enter a username.'
)),
new Regex(array(
'pattern' => '/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/',
'message' => 'Only letters, numbers and underscores allowed in the username'
)),
new StringLength(array(
'max' => 16,
'min' => 4,
'messageMaximum' => 'Username too long. Max. 16 characters allowed.',
'messageMinimum' => 'The username is too short. Please enter at least 4 characters.'
))
));
$this->add($username);
/**
* Validate E-Mail Address
*/
$email = new Text('email', array(
'placeholder' => 'Email'
));
$email->setLabel('E-Mail');
$email->addValidators(array(
new PresenceOf(array(
'message' => 'Please enter an email address.'
)),
new Email(array(
'message' => 'Please enter a valid email address.'
))
));
$this->add($email);
/**
* Validate Password
*/
$password = new Text('password', array(
'placeholder' => 'Password'
));
$password->setLabel('Password');
$password->addValidators(array(
new PresenceOf(array(
'message' => 'You did not enter a password.'
)),
new StringLength(array(
'max' => 40,
'min' => 4,
'messageMaximum' => 'The password must be max 40 characters.',
'messageMinimum' => 'The password must be at least 4 characters.'
))
));
$this->add($password);
}
}