PHP code example of symnedi / validator
1. Go to this page and download the library: Download symnedi/validator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
symnedi / validator example snippets
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\NotBlank
* @Assert\Email
*/
private $email;
/**
* @var string $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
use Symfony\Component\Validator\Validator\ValidatorInterface;
class RegistrationManager
{
/**
* @var ValidatorInterface
*/
private $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
/**
* Instance is passed: $user = new User('invalid.email');
*/
public function registerUser(User $user)
{
$violations = $this->validator->validate($user);
// process violations
$violation = $violations[0];
$violation->getMessage(); // 'Email is not valid.'
}
}