/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (isset($value)) { $url_is_valid = TRUE; /** @var $link_item \Drupal\link\LinkItemInterface */ $link_item = $value; $link_type = $link_item->getFieldDefinition()->getSetting('link_type'); $url_string = $link_item->url; // Validate the url property. if ($url_string !== '') { try { // @todo This shouldn't be needed, but massageFormValues() may not // run. $parsed_url = UrlHelper::parse($url_string); $url = Url::createFromPath($parsed_url['path']); if ($url->isExternal() && !UrlHelper::isValid($url_string, TRUE)) { $url_is_valid = FALSE; } elseif ($url->isExternal() && !($link_type & LinkItemInterface::LINK_EXTERNAL)) { $url_is_valid = FALSE; } } catch (NotFoundHttpException $e) { $url_is_valid = FALSE; } catch (MatchingRouteNotFoundException $e) { $url_is_valid = FALSE; } catch (ParamNotConvertedException $e) { $url_is_valid = FALSE; } } if (!$url_is_valid) { $this->context->addViolation($this->message, array('%url' => $url_string)); } } }
public function checkValidDiaporama($value, ExecutionContextInterface $context) { $value = intval($value); if (is_null(DiaporamaQuery::create()->findOneById($value))) { $context->addViolation($this->trans('diaporama.delete.invalid_diaporama %diaporama_id', array('diaporama_id' => $value))); } }
public function isValid(ExecutionContextInterface $context) { $is_valid = $this->start <= $this->end; if (!$is_valid) { $context->addViolationAt('end', 'bladetester_calendar.validation.event_dates', array(), null); } }
public function verifyExistingEmail($value, ExecutionContextInterface $context) { $customer = CustomerQuery::getCustomerByEmail($value); if ($customer) { $context->addViolation(Translator::getInstance()->trans("This email already exists.")); } }
public function verifyExistingEmail($value, ExecutionContextInterface $context) { $customer = NewsletterQuery::create()->filterByUnsubscribed(false)->findOneByEmail($value); if ($customer) { $context->addViolation(Translator::getInstance()->trans("You are already registered!")); } }
public function verifyCountry($value, ExecutionContextInterface $context) { $address = CountryQuery::create()->findPk($value); if (null === $address) { $context->addViolation(Translator::getInstance()->trans("Country ID not found")); } }
public function verifyCountryList($value, ExecutionContextInterface $context) { $jsonType = new JsonType(); if (!$jsonType->isValid($value)) { $context->addViolation(Translator::getInstance()->trans("Country list is not valid JSON")); } $countryList = json_decode($value, true); foreach ($countryList as $countryItem) { if (is_array($countryItem)) { $country = CountryQuery::create()->findPk($countryItem[0]); if (null === $country) { $context->addViolation(Translator::getInstance()->trans("Country ID %id not found", ['%id' => $countryItem[0]])); } if ($countryItem[1] == "0") { continue; } $state = StateQuery::create()->findPk($countryItem[1]); if (null === $state) { $context->addViolation(Translator::getInstance()->trans("State ID %id not found", ['%id' => $countryItem[1]])); } } else { $context->addViolation(Translator::getInstance()->trans("Wrong country definition")); } } }
public function verifyExistingEmail($value, ExecutionContextInterface $context) { $customer = CustomerQuery::create()->findOneByEmail($value); if (null === $customer) { $context->addViolation(Translator::getInstance()->trans("This email does not exists")); } }
public function verifyExistingLogin($value, ExecutionContextInterface $context) { $administrator = AdminQuery::create()->findOneByLogin($value); if ($administrator !== null) { $context->addViolation("This login already exists"); } }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (isset($value)) { try { /** @var \Drupal\Core\Url $url */ $url = $value->getUrl(); } catch (\InvalidArgumentException $e) { return; } if ($url->isRouted()) { $allowed = TRUE; try { $url->toString(); } catch (RouteNotFoundException $e) { $allowed = FALSE; } catch (InvalidParameterException $e) { $allowed = FALSE; } catch (MissingMandatoryParametersException $e) { $allowed = FALSE; } if (!$allowed) { $this->context->addViolation($constraint->message, array('@uri' => $value->uri)); } } } }
function it_does_not_validate_a_non_existent_channel($channelManager, Channel $constraint, ExecutionContextInterface $context) { $channelManager->getChannelChoices()->willReturn(['mobile' => 'mobile']); $context->addViolation(Argument::cetera())->shouldBeCalled(); $this->initialize($context); $this->validate('Magento', $constraint); }
public function validateSlug($blog, ExecutionContextInterface $context) { $unique = $this->getDoctrineRepo('AppBundle:Blog')->isSlugUnique($blog->getSlug(), $blog->getId()); if (!$unique) { $context->buildViolation('The slug is not unique')->addViolation(); } }
public function isDataValid(ExecutionContextInterface $context) { // valid only on first call if (++self::$validationCalls > 1) { $context->addViolation('Take this!'); } }
/** * Validate a date entered with the current edition Language date format. * * @param string $value * @param ExecutionContextInterface $context */ public function checkDate($value, ExecutionContextInterface $context) { $format = self::PHP_DATE_FORMAT; if (!empty($value) && false === \DateTime::createFromFormat($format, $value)) { $context->addViolation(Translator::getInstance()->trans("Date '%date' is invalid, please enter a valid date using %fmt format", ['%fmt' => self::MOMENT_JS_DATE_FORMAT, '%date' => $value])); } }
public function verifyEmailField($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); if (isset($data["email_confirm"]) && $data["email"] != $data["email_confirm"]) { $context->addViolation(Translator::getInstance()->trans("email confirmation is not the same as email field")); } }
public function checkCityName($value, ExecutionContextInterface $context) { $isValid = InseeGeoMunicipalityQuery::create()->findOneById($value); if (!isset($isValid)) { $context->addViolation(Translator::getInstance()->trans('city.error', [], INSEEGeo::DOMAIN_NAME)); } }
public function verifyTaxList($value, ExecutionContextInterface $context) { $jsonType = new JsonType(); if (!$jsonType->isValid($value)) { $context->addViolation(Translator::getInstance()->trans("Tax list is not valid JSON")); } $taxList = json_decode($value, true); /* check we have 2 level max */ foreach ($taxList as $taxLevel1) { if (is_array($taxLevel1)) { foreach ($taxLevel1 as $taxLevel2) { if (is_array($taxLevel2)) { $context->addViolation(Translator::getInstance()->trans("Bad tax list JSON")); } else { $taxModel = TaxQuery::create()->findPk($taxLevel2); if (null === $taxModel) { $context->addViolation(Translator::getInstance()->trans("Tax ID not found in tax list JSON")); } } } } else { $taxModel = TaxQuery::create()->findPk($taxLevel1); if (null === $taxModel) { $context->addViolation(Translator::getInstance()->trans("Tax ID not found in tax list JSON")); } } } }
public function verifyPasswordField($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); if ($data["password"] != $data["password_confirm"]) { $context->addViolation(Translator::getInstance()->trans("password confirmation is not the same as password field")); } }
/** * Wrapper for {@link ExecutionContextInterface::buildViolation} that * supports the 2.4 context API. * * @param ExecutionContextInterface $context The context to use * @param string $message The violation message * @param array $parameters The message parameters * * @return ConstraintViolationBuilderInterface The violation builder * * @deprecated This method will be removed in Symfony 3.0. */ protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array()) { if ($context instanceof ExecutionContextInterface2Dot5) { return $context->buildViolation($message, $parameters); } return new LegacyConstraintViolationBuilder($context, $message, $parameters); }
public function verifyExistingCode($value, ExecutionContextInterface $context) { $coupon = CouponQuery::create()->findOneByCode($value); if (null === $coupon) { $context->addViolation(Translator::getInstance()->trans("This coupon does not exists")); } }
public function checkDuplicateCode($value, ExecutionContextInterface $context) { $currency = CurrencyQuery::create()->findOneByCode($value); if ($currency) { $context->addViolation(Translator::getInstance()->trans('A currency with code "%name" already exists.', ['%name' => $value])); } }
public function isSubjectValid(ExecutionContextInterface $context) { $text = preg_replace(array('/<a[^>]+href[^>]+>/', '/<\\/a>/'), '', $this->subjectParsed); if (mb_strlen($text, 'utf-8') > 500) { $context->addViolationAt('subject', 'The subject too long'); } }
/** * Validate a date entered with the default Language date format. * * @param string $value * @param ExecutionContextInterface $context */ public function checkLocalizedDate($value, ExecutionContextInterface $context) { $format = LangQuery::create()->findOneByByDefault(true)->getDateFormat(); if (false === \DateTime::createFromFormat($format, $value)) { $context->addViolation(Translator::getInstance()->trans("Date '%date' is invalid, please enter a valid date using %fmt format", ['%fmt' => $format, '%date' => $value])); } }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (isset($value)) { $uri_is_valid = TRUE; /** @var $link_item \Drupal\link\LinkItemInterface */ $link_item = $value; $link_type = $link_item->getFieldDefinition()->getSetting('link_type'); // Try to resolve the given URI to a URL. It may fail if it's schemeless. try { $url = $link_item->getUrl(); } catch (\InvalidArgumentException $e) { $uri_is_valid = FALSE; } // If the link field doesn't support both internal and external links, // check whether the URL (a resolved URI) is in fact violating either // restriction. if ($uri_is_valid && $link_type !== LinkItemInterface::LINK_GENERIC) { if (!($link_type & LinkItemInterface::LINK_EXTERNAL) && $url->isExternal()) { $uri_is_valid = FALSE; } if (!($link_type & LinkItemInterface::LINK_INTERNAL) && !$url->isExternal()) { $uri_is_valid = FALSE; } } if (!$uri_is_valid) { $this->context->addViolation($this->message, array('@uri' => $link_item->uri)); } } }
public function checkRefDifferent($value, ExecutionContextInterface $context) { $originalRef = ProductQuery::create()->filterByRef($value, Criteria::EQUAL)->count(); if ($originalRef !== 0) { $context->addViolation($this->translator->trans('This product reference is already assigned to another product.')); } }
public function checkDuplicateRef($value, ExecutionContextInterface $context) { $count = ProductQuery::create()->filterByRef($value)->count(); if ($count > 0) { $context->addViolation(Translator::getInstance()->trans("A product with reference %ref already exists. Please choose another reference.", array('%ref' => $value))); } }
public function verifyProfileId($value, ExecutionContextInterface $context) { $profile = ProfileQuery::create()->findPk($value); if (null === $profile) { $context->addViolation(Translator::getInstance()->trans("Profile ID not found")); } }
public function verifyTaxId($value, ExecutionContextInterface $context) { $tax = TaxQuery::create()->findPk($value); if (null === $tax) { $context->addViolation("Tax ID not found"); } }
public function checkDuplicateName($value, ExecutionContextInterface $context) { $config = ConfigQuery::create()->findOneByName($value); if ($config) { $context->addViolation(Translator::getInstance()->trans('A variable with name "%name" already exists.', array('%name' => $value))); } }
public function checkAtLeastOnePhoneNumberIsDefined($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); if (empty($data["phone"]) && empty($data["cellphone"])) { $context->addViolationAt("phone", Translator::getInstance()->trans("Please enter a home or mobile phone number")); } }