Hi there,
I'm using Form Manager like in Vokuro app.
I have a forms directory (namespace App\Forms) in application dir where I store all forms used in application. I have no registered service. I create instance of form in controller and set as view variable:
namespace App\Controllers;
use App\Forms\RegisterForm;
class RegisterController extends ControllerBase
{
public function indexAction()
{
$form = new RegisterForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getPost())) {
try {
$user = $this->auth->check($this->request->getPost());
if ($user) {
$this->flashSession->success(_('You have been logged'));
} else {
$this->flashSession->error(_('Incorrect login information'));
}
} catch(\Exception $e) {
$this->flashSession->error(_($e->getMessage()));
return $this->response->redirect('/login');
}
} else {
foreach ($form->getMessages() as $msg) {
$this->flashSession->error(_($msg->getMessage()));
break; // return only first error
}
}
return $this->response->redirect('/');
}
$this->view->form = $form;
}
}
Form Class example:
<?php
namespace App\Forms;
use Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Submit,
Phalcon\Forms\Element\Check,
Phalcon\Forms\Element\Hidden,
Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\Email,
Phalcon\Validation\Validator\Identical;
/**
* Class RegisterForm
* @package App\Forms
*/
class RegisterForm extends BaseForm
{
public function initialize()
{
$email = new Text('email');
$email->setLabel(_('Email'));
$email->addValidators(array(
new PresenceOf(array(
'message' => _('Email is required')
)),
new Email(array(
'message' => _('Email is incorrect')
))
));
$this->add($email);
// Password
$password = new Password('password');
$password->setLabel(_('Password'));
$password->addValidator(
new PresenceOf(array(
'message' => _('Password is required')
))
);
$this->add($password);
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(
new Identical(array(
'value' => $this->security->getSessionToken(),
'message' => _('CSRF validation failed')
))
);
$this->add($csrf);
$this->add(new Submit('submit', array(
'value' => _('Register')
)));
}
}
View template:
{{ form('/login') }}
<div class="popup-header">
<a href="{{ url('/register') }}" class="pull-left"><i class="icon-user-plus"></i></a>
<span class="text-semibold">{{ _('Login') }}</span>
<div class="pull-right">
<span style="display: block; width: 40px; height: 40px;"></span>
</div>
</div>
<div class="well">
<div class="form-group has-feedback">
<label>{{ form.getLabel('email') }}</label>
<input type="text" name="email" class="form-control" placeholder="{{ _('Email') }}">
<i class="icon-users form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label>{{ form.getLabel('password') }}</label>
<input type="password" name="password" class="form-control" placeholder="{{ _('Password') }}">
<i class="icon-lock form-control-feedback"></i>
</div>
<div class="row form-actions">
<div class="col-xs-6">
</div>
<div class="col-xs-6">
<button type="submit" class="btn btn-warning pull-right"><i class="icon-menu2"></i> {{ _('Sign in') }}</button>
</div>
</div>
</div>
{{ endform() }}
Attention:
form("/login")
is default alias for \Phalcon\Tag::form (https://docs.phalcon.io/en/latest/api/Phalcon_Tag.html).
form.render("email")
is call for RegisterForm::render("email"). form
is a view variable name in this case.