Honestly,
I've found the easiest way is to create a form that extends \Phalcon\Forms\Form:
Example of a very simple login form. You can add extra security as you see fit but this is just an example:
<?php
/**
* Login Form
* File: /my/app/path/library/App/Forms/LoginForm.php
*/
namespace App\Forms;
use Phalcon\Forms\Form,
Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Password;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\PresenceOf;
class LoginForm extends Form {
public function initialize()
{
$this->setEntity($this);
$email_address = new Text("email_address");
$email_address->addValidator(new PresenceOf(array(
'message' => 'Email Address is required'
)));
$email_address->setAttribute('class','form-control');
$password = new Password('password');
$password->setAttribute('class','form-control');
$password->addValidator(new PresenceOf(array(
'message' => 'Password is required'
)));
$this->add($email_address);
$this->add($password);
}
}
Then in your controller:
<?php
/**
* Auth Controller
* File: /my/app/path/apps/frontend/controllers/AuthController.php
*/
namespace App\Frontend\Controllers;
use App\Forms\LoginForm;
class AuthController extends ControllerBase
{
public function loginAction()
{
$form = new LoginForm();
// Check to see if this is a POST request
if($this->request->isPost())
{
// Validate the form data posted
if(!$form->isValid($this->request->getPost())){
// If the form failed validation, add the errors to the flash error message.
foreach($form->getMessages() as $message){
$this->flash->error($message->getMessage());
}
} else {
// Form was validated successfully. Lets try to login
$email = $this->request->getPost('email_address', 'email');
$password = $this->request->getPost('password');
$login = \App\Users::authenticate($email, $password);
if ($login) {
// Success! Set your session data here
} else {
// Login Failed!
$this->flash->error('Invalid Login Details');
}
}
}
// Send the form to the view.
$this->view->form = $form;
}
}
And for your view:
<!--
Login View
File: /my/app/path/apps/frontend/views/auth/login.volt
-->
<form method="post" action="/post/url/here">
<label for="email_address">Email Address: </label>
{{ form.render('email_address') }}
<label for="password">Password: </label>
{{ form.render('password') }}
</form>