Created
April 7, 2015 23:08
-
-
Save chrisguitarguy/2b27669a2e8d84d948ff to your computer and use it in GitHub Desktop.
An example of standalone usage of the symfony validator component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/vendor/* | |
/composer.lock | |
# ignore certain files | |
*.pyc | |
*.swp | |
*.dmg | |
*.gz | |
*.iso | |
*.jar | |
*.rar | |
*.tar | |
*.zip | |
*.7z | |
# ignore compiled files | |
*.com | |
*.class | |
*.dll | |
*.exe | |
*.app | |
*.o | |
*.so | |
# system files | |
.DS_Store* | |
ehthumbs.db | |
Icon? | |
Thumbs.db | |
# ignore Sass cache | |
.sass-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "chrisguitarguy/symfony-validation-example", | |
"description": "Standalone validator usage example.", | |
"keywords": ["symfony", "validator"], | |
"require": { | |
"php": ">=5.5", | |
"symfony/validator": "~2.6", | |
"symfony/config": "~2.6" | |
}, | |
"autoload": { | |
"psr-4": { | |
"Chrisguitarguy\\ValidatorExample\\": "." | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\Validator\Validation; | |
use Chrisguitarguy\ValidatorExample\User; | |
$validator = Validation::createValidatorBuilder() | |
->setApiVersion(Validation::API_VERSION_2_5) | |
->addXmlMapping(__DIR__.'/validation.xml') | |
->getValidator(); | |
$user = new User('notAnEmail', null); | |
$errors = $validator->validate($user); | |
foreach ($errors as $error) { | |
echo $error->getPropertyPath(), ': ', $error->getMessage(), PHP_EOL; | |
// or for debugging: | |
echo $error, PHP_EOL; | |
} | |
$validUser = new User('[email protected]', 'aPassword'); | |
$errors = $validator->validate($validUser); | |
echo count($errors), PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Chrisguitarguy\ValidatorExample; | |
final class User | |
{ | |
private $email; | |
private $password; | |
public function __construct($email, $password) | |
{ | |
$this->email = $email; | |
$this->password = $password; | |
} | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | |
<class name="Chrisguitarguy\ValidatorExample\User"> | |
<property name="email"> | |
<constraint name="NotBlank" /> | |
<constraint name="Email" /> | |
</property> | |
<property name="password"> | |
<constraint name="NotBlank" /> | |
</property> | |
</class> | |
</constraint-mapping> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment