本文整理汇总了PHP中Symfony\Component\Validator\Validation类的典型用法代码示例。如果您正苦于以下问题:PHP Validation类的具体用法?PHP Validation怎么用?PHP Validation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Validation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* Runs the validation against the provided array of fields
*
* @param array $data
*/
public function validate(array $data)
{
$this->original_values = $data;
$this->final_values = $data;
$validator = SymfonyValidation::createValidator();
$violations_arr = [];
$this->violations = new ViolationCollection([]);
foreach ($this->constraints as $key => $constraint) {
// we always keep the variables set someway
if (!isset($this->final_values[$key])) {
$this->final_values[$key] = false;
}
// if it isn't an array and we just request a Trim
if (!is_array($constraint) && $constraint instanceof ActiveConstraint) {
$this->final_values[$key] = $constraint->run($this->final_values[$key]);
// it's not a classic symfony constraint, get rid of it
$constraint = [];
}
if (is_array($constraint)) {
foreach ($constraint as $k => $c) {
if ($c instanceof ActiveConstraint) {
$this->final_values[$key] = $c->run($this->final_values[$key]);
// it's not a classic symfony constraint, get rid of it
unset($constraint[$k]);
}
}
}
$violations = $validator->validateValue($this->final_values[$key], $constraint);
if ($violations->count() > 0) {
$violations_arr[$key] = new Violation($violations, $key, $this->labels[$key]);
}
$this->violations = new ViolationCollection($violations_arr);
}
}
示例2: __construct
public function __construct()
{
$builder = Validation::createValidatorBuilder();
$builder->setConstraintValidatorFactory(new ConstraintValidatorFactory());
$builder->setMetadataFactory(new LazyLoadingMetadataFactory(new StaticMethodLoader()));
parent::__construct($builder->getValidator());
}
示例3: getMetaValidator
public function getMetaValidator($allowedLocales = array(), $intlExtension = false, $strict = false)
{
$factory = new ConstraintValidatorFactory($this->getLocaleValidator($intlExtension), $this->getLocaleAllowedValidator($intlExtension, $allowedLocales, $strict));
$validator = Validation::createValidatorBuilder();
$validator->setConstraintValidatorFactory($factory);
return new MetaValidator($validator->getValidator());
}
示例4: register
public function register(Application $app)
{
$app['validator'] = $app->share(function ($app) {
return $app['validator.builder']->getValidator();
});
$app['validator.builder'] = $app->share(function ($app) {
$builder = Validation::createValidatorBuilder();
$builder->setConstraintValidatorFactory($app['validator.validator_factory']);
$builder->setTranslationDomain('validators');
$builder->addObjectInitializers($app['validator.object_initializers']);
$builder->setMetadataFactory($app['validator.mapping.class_metadata_factory']);
if (isset($app['translator'])) {
$builder->setTranslator($app['translator']);
}
return $builder;
});
$app['validator.mapping.class_metadata_factory'] = $app->share(function ($app) {
if (class_exists('Symfony\\Component\\Validator\\Mapping\\Factory\\LazyLoadingMetadataFactory')) {
return new LazyLoadingMetadataFactory(new StaticMethodLoader());
}
return new ClassMetadataFactory(new StaticMethodLoader());
});
$app['validator.validator_factory'] = $app->share(function () use($app) {
return new ConstraintValidatorFactory($app, $app['validator.validator_service_ids']);
});
$app['validator.object_initializers'] = $app->share(function ($app) {
return array();
});
$app['validator.validator_service_ids'] = array();
}
示例5: getExtensions
/**
* @return array
*/
protected function getExtensions()
{
$accountUserRoleSelectType = new EntitySelectTypeStub($this->getRoles(), AccountUserRoleSelectType::NAME, new AccountUserRoleSelectType());
$addressEntityType = new EntityType($this->getAddresses(), 'test_address_entity');
$accountSelectType = new AccountSelectTypeStub($this->getAccounts(), AccountSelectType::NAME);
return [new PreloadedExtension([OroDateType::NAME => new OroDateType(), AccountUserRoleSelectType::NAME => $accountUserRoleSelectType, $accountSelectType->getName() => $accountSelectType, AddressCollectionTypeStub::NAME => new AddressCollectionTypeStub(), $addressEntityType->getName() => $addressEntityType], []), new ValidatorExtension(Validation::createValidator())];
}
示例6: getValidator
/**
* @param Reader $reader
*
* @return ValidatorInterface
*/
public function getValidator(Reader $reader = null)
{
if (null === $reader) {
$reader = new AnnotationReader();
}
return Validation::createValidatorBuilder()->enableAnnotationMapping($reader)->getValidator();
}
示例7: setUp
protected function setUp()
{
parent::setUp();
$this->serializer = SerializerBuilder::create()->build();
$this->validator = Validation::createValidator();
$this->action->setSerializer($this->serializer)->setValidator($this->validator);
}
示例8: validate
public function validate(array $parameters)
{
$validator = Validation::createValidator();
$constraints = new Collection(['method' => new Assert\Required(), 'source_id' => [new Assert\Required(), new Assert\Length(['max' => 45])], 'amount' => [new Assert\GreaterThan(0), new Assert\Required()], 'currency' => [new Assert\Length(3), new Assert\Optional()], 'description' => [new Assert\Length(['max' => 250]), new Assert\Required()], 'order_id' => [new Assert\Length(['max' => 100]), new Assert\Optional()], 'device_session_id' => [new Assert\Length(['max' => 255]), new Assert\Optional()], 'capture' => new Assert\Optional(), 'customer' => new Assert\Optional(), 'metadata' => new Assert\Optional(), 'use_card_points' => new Assert\Optional()]);
$violations = $validator->validate($parameters, $constraints);
return $violations;
}
示例9: register
public function register(Container $app)
{
$app['validator'] = function ($app) {
if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\\Component\\Validator\\Validation');
$file = dirname($r->getFilename()) . '/Resources/translations/validators.' . $app['locale'] . '.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
}
return $app['validator.builder']->getValidator();
};
$app['validator.builder'] = function ($app) {
$builder = Validation::createValidatorBuilder();
$builder->setConstraintValidatorFactory($app['validator.validator_factory']);
$builder->setTranslationDomain('validators');
$builder->addObjectInitializers($app['validator.object_initializers']);
$builder->setMetadataFactory($app['validator.mapping.class_metadata_factory']);
if (isset($app['translator'])) {
$builder->setTranslator($app['translator']);
}
return $builder;
};
$app['validator.mapping.class_metadata_factory'] = function ($app) {
return new LazyLoadingMetadataFactory(new StaticMethodLoader());
};
$app['validator.validator_factory'] = function () use($app) {
$validators = isset($app['validator.validator_service_ids']) ? $app['validator.validator_service_ids'] : array();
return new ConstraintValidatorFactory($app, $validators);
};
$app['validator.object_initializers'] = function ($app) {
return array();
};
}
示例10: validate
public function validate(array $parameters)
{
$validator = Validation::createValidator();
$constraints = new Collection(['token_id' => new Assert\Required(), 'device_session_id' => new Assert\Optional()]);
$violations = $validator->validate($parameters, $constraints);
return $violations;
}
示例11: validate
public function validate(array $parameters)
{
$validator = Validation::createValidator();
$constraints = new Collection(['line1' => new Assert\Required(), 'line2' => new Assert\Optional(), 'line3' => new Assert\Optional(), 'postal_code' => new Assert\Length(['min' => 5]), 'state' => [new Assert\Length(['min' => 2]), new Assert\Required()], 'city' => new Assert\Required(), 'country_code' => [new Assert\Length(['min' => 2, 'max' => 2]), new Assert\Required()]]);
$violations = $validator->validate($parameters, $constraints);
return $violations;
}
示例12: testConstraintsValidatorCanBeModified
public function testConstraintsValidatorCanBeModified()
{
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
Configuration::setConstraintsValidator($validator);
$returnedValidator = Configuration::getConstraintsValidator();
$this->assertEquals($validator, $returnedValidator);
}
示例13: validate
public function validate(array $parameters)
{
$validator = Validation::createValidator();
$constraints = new Collection(['customer_id' => [new Assert\Required(), new Assert\Length(['max' => 45])], 'amount' => [new Assert\GreaterThan(0), new Assert\Required()], 'description' => [new Assert\Length(['max' => 250]), new Assert\Required()], 'order_id' => [new Assert\Length(['max' => 100]), new Assert\Optional()]]);
$violations = $validator->validate($parameters, $constraints);
return $violations;
}
示例14: testPassesValidationWithNullScoreWithForfeit
public function testPassesValidationWithNullScoreWithForfeit()
{
$setScore = new SetScore();
$setScore->setTeamBForfeit();
$validator = Validation::createValidatorBuilder()->addMethodMapping('loadValidatorMetadata')->getValidator();
$this->assertEmpty($validator->validate($setScore));
}
示例15: setUp
/**
* Setting up the validator.
*/
protected function setUp()
{
$this->now = new \DateTime();
$this->validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$this->baseVacancy = new Vacancy();
$this->baseVacancy = $this->baseVacancy->setId(1)->setTitle("this is a title")->setDescription("this is a description for a vacancy")->setStartDate($this->now)->setUrlid("/vacancy/this_is_a_title");
}