WriteConstraintViolationException example

$appIntegrationId = $this->fetchIntegrationIdOfAssociatedApp($command);
            if (!$appIntegrationId) {
                continue;
            }

            if ($integrationId !== $appIntegrationId) {
                $this->addViolation($violationList$command);
            }
        }
        if ($violationList->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
        }
    }

    private function getIntegrationId(Context $context): ?string
    {
        $source = $context->getSource();
        if (!($source instanceof AdminApiSource)) {
            return null;
        }

        return $source->getIntegrationId();
    }
null,
                '/' . $id,
                $id,
                null,
                self::VIOLATION_DELETE_DEFAULT_CURRENCY
            );

            $violations->add($violation);
        }

        if ($violations->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violations));
        }
    }
}
foreach ($states as $record) {
            $id = (string) $record['sales_channel_id'];
            $mapping[$id]['current_default'] = $record['current_default'];
            $mapping[$id]['state'][] = $record['language_id'];
        }

        return $mapping;
    }

    private function writeViolationException(ConstraintViolationList $violations, PreWriteValidationEvent $event): void
    {
        $event->getExceptions()->add(new WriteConstraintViolationException($violations));
    }
}
if (!($command instanceof InsertCommand) || $command->getDefinition()->getClass() !== LandingPageDefinition::class) {
                continue;
            }

            if (!$this->hasAnotherValidCommand($commands$command)) {
                $violationList->addAll(
                    $this->validator->startContext()
                        ->atPath($command->getPath() . '/salesChannels')
                        ->validate(null, [new NotBlank()])
                        ->getViolations()
                );
                $writeException->add(new WriteConstraintViolationException($violationList));
            }
        }
    }

    /** * @param WriteCommand[] $commands */
    private function hasAnotherValidCommand(array $commands, WriteCommand $command): bool
    {
        $isValid = false;
        foreach ($commands as $searchCommand) {
            
$commands = $event->getCommands();
        $affectedIds = $this->getAffectedIds($commands);
        if (\count($affectedIds) === 0) {
            return;
        }

        $violations = new ConstraintViolationList();
        $violations->addAll($this->getInheritanceViolations($affectedIds));
        $violations->addAll($this->getMissingTranslationCodeViolations($affectedIds));

        if ($violations->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violations));
        }
    }

    public function preValidate(PreWriteValidationEvent $event): void
    {
        $commands = $event->getCommands();

        foreach ($commands as $command) {
            $violations = new ConstraintViolationList();

            if ($command instanceof CascadeDeleteCommand || $command->getDefinition()->getClass() !== LanguageDefinition::class) {
                
if (\in_array($scope$field->getAllowedWriteScopes(), true)) {
            return parent::encode($field$existence$data$parameters);
        }

        // In every other case force the user to use a state-transition         $messageTemplate = 'Changing the state-machine-state of this entity is not allowed for scope {{ scope }}. '
            . 'Either change the state-machine-state via a state-transition or use a different scope.';
        $messageParameters = [
            '{{ scope }}' => $scope,
        ];

        throw new WriteConstraintViolationException(new ConstraintViolationList([
            new ConstraintViolation(
                str_replace(array_keys($messageParameters)array_values($messageParameters)$messageTemplate),
                $messageTemplate,
                $messageParameters,
                null,
                '/' . $data->getKey(),
                $data->getValue()
            ),
        ])$parameters->getPath());
    }
}
public function preValidate(PreWriteValidationEvent $event): void
    {
        if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
            return;
        }

        $violations = new ConstraintViolationList();
        $violations->addAll($this->getDeletedSystemTranslationViolations($event->getCommands()));

        if ($violations->count()) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violations));
        }
    }

    /** * @param list<WriteCommand> $writeCommands */
    private function getDeletedSystemTranslationViolations(array $writeCommands): ConstraintViolationList
    {
        $violations = new ConstraintViolationList();

        foreach ($writeCommands as $writeCommand) {
            
$violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        if (\count($violationList)) {
            throw new WriteConstraintViolationException($violationList$path);
        }
    }

    protected function requiresValidation(
        Field $field,
        EntityExistence $existence,
        $value,
        WriteParameterBag $parameters
    ): bool {
        if ($value !== null) {
            return true;
        }
$this->buildViolation(
                        'The defined tax rate is required when fixed tax present',
                        ['{{ taxId }}' => null],
                        '/taxId',
                        $taxType,
                        self::VIOLATION_TAX_ID_REQUIRED
                    )
                );
            }

            if ($violations->count() > 0) {
                $event->getExceptions()->add(new WriteConstraintViolationException($violations$command->getPath()));
            }
        }
    }

    /** * @return array<string, mixed> */
    private function findShippingMethod(string $shippingMethodId): array
    {
        $shippingMethod = $this->connection->executeQuery(
            'SELECT `tax_type`, `tax_id` FROM `shipping_method` WHERE `id` = :id',
            [
$this->validateProperties(
                        $value,
                        AbsolutePriceDefinition::getConstraints(),
                        $parameters->getPath()
                    );

                    if (!\array_key_exists('filter', $value) || $value['filter'] === null) {
                        break;
                    }
                    $violations = $this->validateRules($value['filter']$parameters->getPath() . '/filter');
                    if ($violations->count() > 0) {
                        throw new WriteConstraintViolationException($violations$parameters->getPath());
                    }

                    break;
                case CurrencyPriceDefinition::TYPE:
                    $this->validateProperties(
                        $value,
                        CurrencyPriceDefinition::getConstraints(),
                        $parameters->getPath()
                    );
                    if (!\array_key_exists('filter', $value) || $value['filter'] === null) {
                        break;
                    }
$violationList->add(
            new ConstraintViolation(
                'No price for default currency defined',
                'No price for default currency defined',
                [],
                '',
                '/price',
                $prices
            )
        );

        throw new WriteConstraintViolationException($violationList$parameters->getPath());
    }
}
try {
            $ruleInstance = $this->ruleConditionRegistry->getRuleInstance($type);
        } catch (InvalidConditionException) {
            $violation = $this->buildViolation(
                'This {{ value }} is not a valid condition type.',
                ['{{ value }}' => $type],
                '/type',
                'CONTENT__INVALID_RULE_TYPE_EXCEPTION'
            );
            $violationList->add($violation);
            $writeException->add(new WriteConstraintViolationException($violationList$command->getPath()));

            return;
        }

        $value = $this->getConditionValue($condition$payload);

        // add violations when a property is not defined on the rule instance         $missingProperties = array_filter(
            $value,
            static fn (string $key): bool => !property_exists($ruleInstance$key),
            \ARRAY_FILTER_USE_KEY
        );
$discount,
                        $command->getPayload(),
                        $violationList,
                        $index
                    );

                    break;
            }
        }

        if ($violationList->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
        }
    }

    /** * This function collects all database data that might be * required for any of the received entities and values. * * @param array<WriteCommand> $writeCommands * * @throws PromotionException * @throws Exception */
sprintf($message$entity),
                sprintf($message, '{{ entity }}'),
                ['{{ entity }}' => $entity],
                null,
                '/',
                null,
                null,
                self::VIOLATION_LOCKED
            ));
        }

        $event->getExceptions()->add(new WriteConstraintViolationException($violations));
    }

    /** * @param WriteCommand[] $writeCommands * * @return array<string, bool> */
    private function containsLockedEntities(array $writeCommands): array
    {
        $ids = [];
        $locked = [];

        
continue;
            }

            if (!isset($command->getPayload()['type'])) {
                continue;
            }

            $violationList->addAll($this->checkTypeChange($command$event));
        }

        if ($violationList->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violationList));

            return;
        }
    }

    private function checkTypeChange(WriteCommand $command, PostWriteValidationEvent $event): ConstraintViolationListInterface
    {
        $violationList = new ConstraintViolationList();
        $payload = $command->getPayload();

        if (\in_array($payload['type'], self::ALLOWED_TYPE_CHANGE, true)) {
            
Home | Imprint | This part of the site doesn't use cookies.