Skip to content

Instantly share code, notes, and snippets.

@dereckmartin
Last active October 20, 2016 05:14
Show Gist options
  • Save dereckmartin/b5650b1943e8b0b0bed1baeae9cc78c3 to your computer and use it in GitHub Desktop.
Save dereckmartin/b5650b1943e8b0b0bed1baeae9cc78c3 to your computer and use it in GitHub Desktop.
Snippet of using validation and filters for form submission data using phalcon
<?php
/**
* Snippet of using validation and filters for form submission
* data using phalcon.
*
* The "simple" project was created using phalcon dev tools
*/
/**
* app/config/config.php
*/
return new \Phalcon\Config([
'application' => [
// add
'validatorsDir' => APP_PATH . '/validators/',
]
]);
/**
* app/config/loader.php
*/
$loader->registerDirs(
[
// add
$config->application->validatorsDir
]
)->register();
/**
* app/validators/RegisterFormValidator.php
*/
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\PresenceOf;
class RegisterFormValidator extends Validation
{
public function initialize()
{
$this->add("username",
new Email([
"message" => "The username is not valid",
])
);
$this->add("username",
new PresenceOf([
"message" => "The username is required",
])
);
$this->add("password",
new PresenceOf([
"message" => "The password is required",
])
);
}
}
/**
* app/controllers/SessionController.php
*/
class SessionController extends \Phalcon\Mvc\Controller
{
public function registerAction()
{
if ( $this->request->isPost() ) {
$validation = new RegisterFormValidator();
$messages = $validation
->setFilters( "username", [ "trim", "lower", "email", "striptags" ] )
->setFilters( "password", [ "trim", "string" ] )
->validate( $this->request->getPost() );
foreach ( $messages as $message ) {
$this->flash->error($message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment