本文整理汇总了PHP中Symfony\Component\Validator\ExecutionContextInterface::addViolation方法的典型用法代码示例。如果您正苦于以下问题:PHP ExecutionContextInterface::addViolation方法的具体用法?PHP ExecutionContextInterface::addViolation怎么用?PHP ExecutionContextInterface::addViolation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\ExecutionContextInterface
的用法示例。
在下文中一共展示了ExecutionContextInterface::addViolation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* {@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));
}
}
}
示例2: validate
/**
* {@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));
}
}
}
示例3: verifyCountryList
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"));
}
}
}
示例4: validate
/**
* {@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));
}
}
}
}
示例5: verifyTaxList
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"));
}
}
}
}
示例6: validate
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint)
{
if (isset($items)) {
$date_values = $items->getValue();
if ($date_values[0]['value'] >= $date_values[1]['value']) {
$this->context->addViolation($constraint->message);
}
}
}
示例7: setMessage
/**
* Wrapper for $this->context->addViolation()
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
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);
}
示例8: verifyDeliveryModule
public function verifyDeliveryModule($value, ExecutionContextInterface $context)
{
$module = ModuleQuery::create()->filterActivatedByTypeAndId(BaseModule::DELIVERY_MODULE_TYPE, $value)->findOne();
if (null === $module) {
$context->addViolation(Translator::getInstance()->trans("Delivery module ID not found"));
} elseif (!$module->isDeliveryModule()) {
$context->addViolation(sprintf(Translator::getInstance()->trans("delivery module %s is not a Thelia\\Module\\DeliveryModuleInterface"), $module->getCode()));
}
}
示例9: verifyPasswordField
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"));
}
if ($data["password"] !== '' && strlen($data["password"]) < 4) {
$context->addViolation(Translator::getInstance()->trans("password must be composed of at least 4 characters"));
}
}
示例10: setMessage
/**
* Wrapper for $this->context->addViolation()
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
protected function setMessage($template, array $parameters = array())
{
trigger_error('setMessage() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
$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);
}
示例11: hasCorrectBaseHref
/**
* @param string $url referrer url to check against base href of this application
* @param \Symfony\Component\Validator\ExecutionContextInterface $context
*/
public function hasCorrectBaseHref($url, ExecutionContextInterface $context)
{
if (!is_string($url)) {
$context->addViolation('URL is not a string.');
} else {
$base_href = $this->getContext()->getRouting()->getBaseHref();
if (strpos($url, "{$base_href}", 0) !== 0) {
$context->addViolation('URL does not start with base href of this application. Same origin violation.');
}
}
}
示例12: checkStateId
public function checkStateId($value, ExecutionContextInterface $context)
{
if ($value['migrate']) {
if (null !== ($state = StateQuery::create()->findPk($value['new_state']))) {
if ($state->getCountryId() !== $value['new_country']) {
$context->addViolation(Translator::getInstance()->trans("The state id '%id' does not belong to country id '%id_country'", ['%id' => $value['new_state'], '%id_country' => $value['new_country']]));
}
} else {
$context->addViolation(Translator::getInstance()->trans("The state id '%id' doesn't exist", ['%id' => $value['new_state']]));
}
}
}
示例13: checkEmails
public function checkEmails($value, ExecutionContextInterface $context)
{
$data = $context->getRoot()->getData();
$value = trim($value);
if ("" === trim($value) && !empty($data["enabled"])) {
$context->addViolation($this->trans("The Emails can not be empty", ["%id" => $value]));
}
$emails = explode(',', $value);
foreach ($emails as $email) {
if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
$context->addViolation($this->trans("'%email' is not a valid email address", ["%email" => $email]));
}
}
}
示例14: verifyPasswordField
public function verifyPasswordField($value, ExecutionContextInterface $context)
{
$data = $context->getRoot()->getData();
if ($data["password"] === '' && $data["password_confirm"] === '') {
$context->addViolation("password can't be empty");
}
if ($data["password"] != $data["password_confirm"]) {
$context->addViolation("password confirmation is not the same as password field");
}
$minLength = ConfigQuery::getMinimuAdminPasswordLength();
if (strlen($data["password"]) < $minLength) {
$context->addViolation("password must be composed of at least {$minLength} characters");
}
}
示例15: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (isset($value)) {
try {
/** @var \Drupal\Core\Url $url */
$url = $value->getUrl();
} catch (\InvalidArgumentException $e) {
return;
}
// Disallow external URLs using untrusted protocols.
if ($url->isExternal() && !in_array(parse_url($url->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
$this->context->addViolation($constraint->message, array('@uri' => $value->uri));
}
}
}