public function testAddViolationAtUsesPassedNullValue() { $this->translator->expects($this->once())->method('trans')->with('Error', array('foo' => 'bar'))->will($this->returnValue('Translated error')); $this->translator->expects($this->once())->method('transChoice')->with('Choice error', 2, array('foo' => 'bar'))->will($this->returnValue('Translated choice error')); // passed null value should override preconfigured value "invalid" $this->context->addViolationAt('bam.baz', 'Error', array('foo' => 'bar'), null); $this->context->addViolationAt('bam.baz', 'Choice error', array('foo' => 'bar'), null, 2); $this->assertEquals(new ConstraintViolationList(array(new ConstraintViolation('Translated error', 'Error', array('foo' => 'bar'), 'Root', 'foo.bar.bam.baz', null), new ConstraintViolation('Translated choice error', 'Choice error', array('foo' => 'bar'), 'Root', 'foo.bar.bam.baz', null, 2))), $this->context->getViolations()); }
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('*****@*****.**'); $userRepository->findOneByEmail('*****@*****.**')->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); }
/** * 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.'); } }
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); } }
public function isRegionValid(ExecutionContext $context) { if ($this->getCountry() && $this->getCountry()->hasRegions() && !$this->region && !$this->regionText) { // do not allow saving text region in case when region was checked from list // except when in base data region text existed // another way region_text field will be null, logic are placed in form listener $propertyPath = $context->getPropertyPath() . '.region'; $context->addViolationAt($propertyPath, 'Region is required for country %country%', array('%country%' => $this->getCountry()->getName())); } }
public function isRegionValid(ExecutionContext $context) { if ($this->getCountry() && $this->getCountry()->hasRegions() && !$this->state) { $propertyPath = $context->getPropertyPath() . '.state'; $context->addViolationAt($propertyPath, 'State is required for country %country%', array('%country%' => $this->getCountry()->getName())); } }
/** * Custom validation constraint * Not valid if no one recipient specified * * @param ExecutionContext $context */ public function isValid(ExecutionContext $context) { $notValid = $this->getGroups()->isEmpty() && $this->getUsers()->isEmpty() && $this->getEmail() == null && $this->getOwner() == null; if ($notValid) { $propertyPath = $context->getPropertyPath() . '.recipientList'; $context->addViolationAt($propertyPath, 'oro.notification.validators.recipient_list.empty.message'); } }
public function isPackageUnique(ExecutionContext $context) { try { if ($this->entityRepository->findOneByName($this->name)) { $context->addViolationAt('repository', 'A package with the name <a href="' . $this->router->generate('view_package', array('name' => $this->name)) . '">' . $this->name . '</a> already exists.', array(), null); } } catch (\Doctrine\ORM\NoResultException $e) { } }
/** * validate * * @param type $data * @param ExecutionContext $context */ public function validate($data, ExecutionContext $context) { if ($data['mode'] === 'add_favorite') { if (!$this->security->isGranted('ROLE_USER')) { $context->addViolationAt('', 'ログインしてください.'); } } else { $context->validateValue($data['product_class_id'], array(new Assert\NotBlank()), '[product_class_id]'); if ($this->Product->getClassName1()) { $context->validateValue($data['classcategory_id1'], array(new Assert\NotBlank(), new Assert\NotEqualTo(array('value' => '__unselected', 'message' => 'This value should be blank.'))), '[classcategory_id1]'); } if ($this->Product->getClassName2()) { $context->validateValue($data['classcategory_id2'], array(new Assert\NotBlank(), new Assert\NotEqualTo(array('value' => '__unselected', 'message' => 'This value should be blank.'))), '[classcategory_id2]'); } } }
public function isExpireDateValid(ExecutionContext $context) { $now = new \DateTime(); $thisYear = (int) $now->format('Y'); $thisMonth = (int) $now->format('m'); $expYear = (int) $this->getExpiryDateYear(); $expMonth = (int) $this->getExpiryDateMonth(); if ($thisYear > $expYear || $thisYear == $expYear && $thisMonth > $expMonth) { $context->addViolationAt('expiry_date_month', 'expired card', array(), null); } }
public function isUnderTotalConstraint(ExecutionContext $context) { $this->getFreeTotal($credit, $debit); // compare with an epsilon, we're using floats here! if ($credit < -0.001) { $context->addViolationAt('credit', 'Le total des factures dépasse le crédit de la ligne de %depassement% €', array('%depassement%' => round($credit, 2) * -1), null); } if ($debit < -0.001) { $context->addViolationAt('debit', 'Le total des factures dépasse le débit de la ligne de %depassement% €', array('%depassement%' => round($debit, 2) * -1), null); } }
public function checkPasswordLength(ExecutionContext $context) { if ($this->can_credentials === true && mb_strlen($this->password, 'UTF-8') < 6) { $context->addViolationAt('password', "Le mot de passe doit faire au minimum 6 caractères"); } }
public function isTvasCorrect(ExecutionContext $context) { $montant_ht = 0; foreach ($this->tvas as $tva) { $montant_ht += $tva->getMontantHt(); } if (abs($this->montant - $montant_ht) > 0.001) { // floats -> epsilon! $context->addViolationAt('tvas', 'La somme des montants HT doit être égale au montant de la facture'); } }
public function isPhoneValid(ExecutionContext $context) { if (!$this->getPhoneNumber() && !$this->getContactPhoneNumber()) { $propertyPath = $context->getPropertyPath() . '.contactPhoneNumber'; $context->addViolationAt($propertyPath, 'orocrm.call.phone.required.message'); } }