php Symfony-Component-Validator-ExecutionContext类(方法)实例源码

下面列出了php Symfony-Component-Validator-ExecutionContext 类(方法)源码代码实例,从而了解它的用法。

作者:cjsissing    项目:calendar-bundl   
public function isValid(ExecutionContext $context)
 {
     $is_valid = $this->start <= $this->end;
     if (!$is_valid) {
         $context->addViolationAtSubPath('end', 'cjsissingh_calendar.validation.event_dates', array(), null);
     }
 }

作者:rampell    项目:FOSCommentBundl   
/**
  * {@inheritdoc}
  */
 public function isVoteValid(ExecutionContext $context)
 {
     if (!$this->checkValue($this->value)) {
         $message = 'A vote cannot have a 0 value';
         $propertyPath = $context->getPropertyPath() . '.value';
         $context->addViolationAtPath($propertyPath, $message);
     }
 }

作者:radzikowsk    项目:CraftyComponent   
/**
  * {@inheritdoc}
  */
 public function isVoteValid(ExecutionContext $context)
 {
     if (!$this->checkValue($this->value)) {
         $propertyPath = $context->getPropertyPath() . '.value';
         $context->setPropertyPath($propertyPath);
         $context->addViolation('A vote cannot have a 0 value', array(), null);
     }
 }

作者:nickaggarwa    项目:sample-symfony   
protected function setUp()
 {
     $walker = $this->getMock('Symfony\\Component\\Validator\\GraphWalker', array(), array(), '', false);
     $factory = $this->getMock('Symfony\\Component\\Validator\\Mapping\\ClassMetadataFactoryInterface');
     $context = new ExecutionContext('root', $walker, $factory);
     $context->setCurrentClass(__CLASS__);
     $this->validator = new ChoiceValidator();
     $this->validator->initialize($context);
 }

作者:richardmille    项目:symfon   
/**
  * Wrapper for $this->context->addViolation()
  *
  * @deprecated
  */
 protected function setMessage($template, array $parameters = array())
 {
     $this->messageTemplate = $template;
     $this->messageParameters = $parameters;
     if (!$this->context instanceof ExecutionContext) {
         throw new ValidatorException('ConstraintValidator::initialize() must be called before setting violation messages');
     }
     $this->context->addViolation($template, $parameters);
 }

作者:geoffreytra    项目:zy   
public function validateValueAsYaml(ExecutionContext $context)
 {
     try {
         Inline::load($this->value);
     } catch (ParserException $e) {
         $context->setPropertyPath($context->getPropertyPath() . '.value');
         $context->addViolation('This value is not valid YAML syntax', array(), $this->value);
     }
 }

作者:javiersanto    项目:pim-community-de   
function it_does_not_validate_attribute_with_non_string_options(AbstractAttribute $attribute, ExecutionContext $context, AttributeOption $option1, AttributeOption $option2, AttributeOption $option3, AttributeOption $option4)
 {
     $option1->getCode()->willReturn('ab');
     $option2->getCode()->willReturn(0);
     $option3->getCode()->willReturn('ef');
     $option4->getCode()->willReturn('gh');
     $attribute->getOptions()->willReturn([$option1, $option2, $option3, $option4]);
     $context->addViolation('Code must be a string. Type "integer" found.')->shouldBeCalled();
     $this->areOptionsValid($attribute, $context);
 }

作者:TheMadelein    项目:Syliu   
function it_validates_if_user_with_given_email_is_already_registered(UserRepository $userRepository, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authorizationChecker, ExecutionContext $context, TokenInterface $token, UniqueReviewerEmail $constraint, ReviewInterface $review, CustomerInterface $customer, CustomerInterface $existingUser)
 {
     $tokenStorage->getToken()->willReturn($token);
     $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')->willReturn(false);
     $review->getAuthor()->willReturn($customer);
     $customer->getEmail()->willReturn('john.doe@example.com');
     $userRepository->findOneByEmail('john.doe@example.com')->willReturn($existingUser);
     $constraint->message = 'This email is already registered. Please log in.';
     $context->addViolationAt('author', 'This email is already registered. Please log in.', [], null)->shouldBeCalled();
     $this->validate($review, $constraint);
 }

作者:rvaloi    项目:Tutorial-Symfony2-Sd   
public function isEmailValid(ExecutionContext $context)
 {
     // somehow you have an array of "fake email"
     $fakeEmails = array('test@test.com');
     // check if the name is actually a fake email
     if (in_array($this->getEmail(), $fakeEmails)) {
         $propertyPath = $context->getPropertyPath() . '.email';
         $context->setPropertyPath($propertyPath);
         $context->addViolation('Tu ne te moquerais pas un peu de moi avec cet email ?', array(), null);
     }
 }

作者:pborrel    项目:composer-servic   
public function it_does_not_add_violation_if_the_given_value_is_valid_json(ExecutionContext $context, ComposerJson $constraint)
    {
        $composerJsonContent = <<<'EOT'
{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}
EOT;
        $context->addViolation(Argument::any())->shouldNotBeCalled();
        $this->validate($composerJsonContent, $constraint);
    }

作者:rouff    项目:symfon   
public static function validateFormChildren(FormInterface $form, ExecutionContext $context)
 {
     if ($form->getAttribute('cascade_validation')) {
         $propertyPath = $context->getPropertyPath();
         $graphWalker = $context->getGraphWalker();
         // Adjust the property path accordingly
         if (!empty($propertyPath)) {
             $propertyPath .= '.';
         }
         $propertyPath .= 'children';
         $graphWalker->walkReference($form->getChildren(), Constraint::DEFAULT_GROUP, $propertyPath, true);
     }
 }

作者:rodgerm    项目:mura-show.co   
public function areImagesValid(ExecutionContext $context)
 {
     $captured_ids = array_map(function ($image) {
         return $image->getId();
     }, $this->images->toArray());
     $property_path = $context->getPropertyPath() . '.images';
     if (!count($captured_ids)) {
         $context->addViolationAt($property_path, 'Please select at least one image!', array(), null);
         return;
     }
     $count = $this->query_builder->andWhere($this->query_builder->expr()->in('i.id', $captured_ids))->select('COUNT(i.id)')->getQuery()->getSingleScalarResult();
     if (!$count) {
         $context->addViolation('Please select images from the list!', array(), null);
     }
 }

作者:javiersanto    项目:pim-community-de   
/**
  * Validation rule for attribute option values
  *
  * @param AbstractAttribute $attribute
  * @param ExecutionContext  $context
  */
 public static function areOptionsValid(AbstractAttribute $attribute, ExecutionContext $context)
 {
     $existingValues = array();
     foreach ($attribute->getOptions() as $option) {
         $code = $option->getCode();
         if (isset($existingValues[$code])) {
             $context->addViolation(self::VIOLATION_DUPLICATE_OPTION_CODE);
         } elseif ($code === null) {
             $context->addViolation(self::VIOLATION_OPTION_CODE_REQUIRED);
         } elseif (!is_string($code)) {
             $context->addViolation(self::VIOLATION_NON_STRING_CODE . sprintf(' Type "%s" found.', gettype($code)));
         } else {
             $existingValues[$code] = '';
         }
     }
 }

作者:ramunas    项目:platfor   
public function testNotExistingFormData()
 {
     $formConfig = $this->getMock('\\Symfony\\Component\\Form\\FormConfigInterface');
     $form = new Form($formConfig);
     $this->context = $this->getMock('\\Symfony\\Component\\Validator\\ExecutionContextInterface');
     $this->context->expects($this->any())->method('getRoot')->will($this->returnValue($form));
     $this->validator->initialize($this->context);
     $this->assertNull($this->validator->validate(false, $this->constraint));
 }

作者:rfc148    项目:blo   
/**
  * Validates the data of a form
  *
  * This method is called automatically during the validation process.
  *
  * @param FormInterface    $form    The validated form
  * @param ExecutionContext $context The current validation context
  */
 public static function validateFormData(FormInterface $form, ExecutionContext $context)
 {
     if (is_object($form->getData()) || is_array($form->getData())) {
         $propertyPath = $context->getPropertyPath();
         $graphWalker = $context->getGraphWalker();
         // The Execute constraint is called on class level, so we need to
         // set the property manually
         $context->setCurrentProperty('data');
         // Adjust the property path accordingly
         if (!empty($propertyPath)) {
             $propertyPath .= '.';
         }
         $propertyPath .= 'data';
         foreach (self::getFormValidationGroups($form) as $group) {
             $graphWalker->walkReference($form->getData(), $group, $propertyPath, true);
         }
     }
 }

作者:daird    项目:cr   
/**
  * Validates contact method
  *
  * @param ContactRequest   $object
  * @param ExecutionContext $context
  */
 public static function validate(ContactRequest $object, ExecutionContext $context)
 {
     $emailError = $phoneError = false;
     switch ($object->getPreferredContactMethod()) {
         case ContactRequest::CONTACT_METHOD_PHONE:
             $phoneError = !$object->getPhone();
             break;
         case ContactRequest::CONTACT_METHOD_EMAIL:
             $emailError = !$object->getEmailAddress();
             break;
         case ContactRequest::CONTACT_METHOD_BOTH:
         default:
             $phoneError = !$object->getPhone();
             $emailError = !$object->getEmailAddress();
     }
     if ($emailError) {
         $context->addViolationAt('emailAddress', 'This value should not be blank.');
     }
     if ($phoneError) {
         $context->addViolationAt('phone', 'This value should not be blank.');
     }
 }

作者:neokenso    项目:symfon   
public static function validateFormChildren(FormInterface $form, ExecutionContext $context)
 {
     if ($form->getAttribute('cascade_validation')) {
         $propertyPath = $context->getPropertyPath();
         $graphWalker = $context->getGraphWalker();
         // The Execute constraint is called on class level, so we need to
         // set the property manually
         $context->setCurrentProperty('children');
         // Adjust the property path accordingly
         if (!empty($propertyPath)) {
             $propertyPath .= '.';
         }
         $propertyPath .= 'children';
         foreach (self::getFormValidationGroups($form) as $group) {
             $graphWalker->walkReference($form->getChildren(), $group, $propertyPath, true);
         }
     }
 }

作者:nickaggarwa    项目:sample-symfony   
public function testValidateDataSetsCurrentPropertyToData()
 {
     $graphWalker = $this->createMockGraphWalker();
     $metadataFactory = $this->createMockMetadataFactory();
     $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
     $object = $this->getMock('\\stdClass');
     $form = new Form('author');
     $test = $this;
     $graphWalker->expects($this->once())->method('walkReference')->will($this->returnCallback(function () use($context, $test) {
         $test->assertEquals('data', $context->getCurrentProperty());
     }));
     $form->setData($object);
     $form->validateData($context);
 }

作者:brookinsconsultin    项目:ezecosyste   
/**
  * Validates a value against a constraint.
  *
  * @param Constraint $constraint
  * @param            $value
  * @param            $group
  * @param            $propertyPath
  * @param null       $currentClass
  * @param null       $currentProperty
  *
  * @deprecated Deprecated since version 2.2, to be removed in 2.3.
  */
 public function walkConstraint(Constraint $constraint, $value, $group, $propertyPath, $currentClass = null, $currentProperty = null)
 {
     trigger_error('walkConstraint() is deprecated since version 2.2 and will be removed in 2.3.', E_USER_DEPRECATED);
     $metadata = null;
     // BC code to make getCurrentClass() and getCurrentProperty() work when
     // called from within this method
     if (null !== $currentClass) {
         $metadata = $this->metadataFactory->getMetadataFor($currentClass);
         if (null !== $currentProperty && $metadata instanceof PropertyMetadataContainerInterface) {
             $metadata = current($metadata->getPropertyMetadata($currentProperty));
         }
     }
     $context = new ExecutionContext($this->visitor, $this->translator, $this->translationDomain, $metadata, $value, $group, $propertyPath);
     $context->validateValue($value, $constraint);
 }

作者:shomim    项目:builde   
public function testGetPropertyPathWithNestedCollectionsAndAllMixed()
 {
     $constraints = new Collection(array('shelves' => new All(array('constraints' => array(new Collection(array('name' => new ConstraintA(), 'books' => new All(array('constraints' => array(new ConstraintA())))))))), 'name' => new ConstraintA()));
     $data = array('shelves' => array(array('name' => 'Research', 'books' => array('foo', 'bar')), array('name' => 'VALID', 'books' => array('foozy', 'VALID', 'bazzy'))), 'name' => 'Library');
     $expectedViolationPaths = array('[shelves][0][name]', '[shelves][0][books][0]', '[shelves][0][books][1]', '[shelves][1][books][0]', '[shelves][1][books][2]', '[name]');
     $visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), $this->translator);
     $context = new ExecutionContext($visitor, $this->translator, self::TRANS_DOMAIN);
     $context->validateValue($data, $constraints);
     foreach ($context->getViolations() as $violation) {
         $violationPaths[] = $violation->getPropertyPath();
     }
     $this->assertEquals($expectedViolationPaths, $violationPaths);
 }


问题


面经


文章

微信
公众号

扫码关注公众号