Phalcon support 2 validation components:
Phalcon\Validation\Validator
Phalcon\Mvc\Model\Validator
I dont know how to use them in my situation. I have a registration form with:
- csrf
- username
- password
- repeat password
- repeat email
- submit button
I created a registration form as following:
class RegistrationForm extends \Phalcon\Forms\Form
{
public function initialize()
{
$csrf = new \Phalcon\Forms\Element\Hidden('csrf');
$csrf->addValidator(new \Phalcon\Validation\Validator\Identical(array(
'value' => $this->security->getSessionToken(),
'message' => 'CSRF validation failed'
)));
$username = new \Phalcon\Forms\Element\Text('username');
$username->addFilter('trim');
$username->addValidator(new PresenceOf(array(
'message' => 'Username is required.',
)));
$username->addValidator(new StringLength(array(
'min' => 6,
'messageMinimum' => 'Username must be at least 6 characters.'
)));
// ...
}
}
And this is my controller/action:
class UserController extends \Phalcon\Mvc\Controller
{
public function registerAction()
{
$form = new RegistrationForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getPost())) {
// Test only
var_dump($this->request->getPost());
exit;
} else {
// Test only
foreach ($form->getMessages() as $message) {
echo $message, '<br/>';
}
exit;
}
}
$this->view->form = $form;
}
}
- When we use model validator and vice versa?
- Can we combine them and how?
- I want to know the right way to implement this register action using model and non-model validator to check csrf, identical password, username and email uniqueness.
Thank for your help!