ValidationException example



        $this->validateField('email', $customer->getEmail()[
            new CustomerEmail([
                'shop' => $this->context->getShopContext()->getShop(),
                'customerId' => $customer->getId(),
                'accountMode' => $customer->getAccountMode(),
            ]),
        ]);

        if ($this->validationContext->getViolations()->count() > 0) {
            throw new ValidationException($this->validationContext->getViolations());
        }
    }

    /** * {@inheritdoc} */
    public function isValid(Customer $customer)
    {
        try {
            $this->validate($customer);
        } catch (ValidationException $ex) {
            
new NoUrl(),
        ]);
        $this->validateField('name', $user->getName()[new NotBlank()new NoUrl()]);
        $this->validateField('role', $user->getRole()[new NotBlank()]);
        $this->validateField('email', $user->getEmail()[
            new UserEmail([
                'userId' => $user->getId(),
            ]),
        ]);

        if ($this->validationContext->getViolations()->count()) {
            throw new ValidationException($this->validationContext->getViolations());
        }
    }

    /** * {@inheritdoc} */
    public function isValid(User $user)
    {
        try {
            $this->validate($user);
        } catch (ValidationException $ex) {
            
$variant = $this->getRepository()->find($id);

        if (!$variant instanceof Detail) {
            throw new NotFoundException(sprintf('Variant by id %d not found', $id));
        }

        $variant = $this->internalUpdate($id$params$variant->getArticle());

        $violations = $this->getManager()->validate($variant);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->flush();

        return $variant;
    }

    /** * Creates a new variant for an product. * This function requires an articleId in the params parameter. * * @throws ValidationException * @throws NotFoundException * @throws ParameterMissingException * * @return Detail */
$order->setCustomerComment('');
        $order->setInternalComment('');
        $order->setTemporaryId('');
        $order->setTransactionId('');
        $order->setTrackingCode('');
        $order->setReferer('');

        $order->fromArray($params);

        $violations = $this->getManager()->validate($order);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->createAddresses($params$order);

        // Generate an order number if none was provided. Doing it after validation since         // the generation of the order number cannot be reverted in a simple manner.         if ($order->getNumber() === null) {
            $orderNumberGenerator = Shopware()->Container()->get(NumberRangeIncrementerInterface::class);
            $orderNumber = $orderNumberGenerator->increment('invoice');

            $order->setNumber((string) $orderNumber);
            
$category->fromArray($params);

        if (isset($params['id'])) {
            $metaData = $this->getManager()->getMetadataFactory()->getMetadataFor(CategoryModel::class);
            $metaData->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
            $category->setPrimaryIdentifier($params['id']);
        }

        $violations = $this->getManager()->validate($category);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($category);
        $this->flush();

        if (!empty($params['translations'])) {
            $this->writeTranslations($category->getId()$params['translations']);
        }

        return $category;
    }

    


        $data['requiredPlugins'] = json_encode($data['requiredPlugins']);

        // slugify technical name of preset         $data['name'] = $this->slugService->slugify($data['name']);
        $preset->fromArray($data);
        $this->validateName($preset);

        $violations = $this->models->validate($preset);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->models->persist($preset);
        $this->models->flush();

        return $preset;
    }

    /** * @throws CustomValidationException */
    
$params = $this->prepareCustomerGroupData($params);

        $result = new Group();

        $discounts = $params['discounts'];
        unset($params['discounts']);

        $result->fromArray($params);

        $violations = $this->getManager()->validate($result);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($result);

        $this->saveDiscounts($discounts$result);

        $this->flush();

        return $result;
    }

    
$payment = new PaymentModel();

        $payment->setAdditionalDescription('');

        $params = $this->preparePaymentData($params);

        $payment->fromArray($params);

        $violations = $this->getManager()->validate($payment);

        if ($violations->count() > 0) {
            throw new ApiException\ValidationException($violations);
        }

        $this->getManager()->persist($payment);
        $this->flush();

        return $payment;
    }

    /** * @param int $id * * @throws \Shopware\Components\Api\Exception\ValidationException * @throws \Shopware\Components\Api\Exception\NotFoundException * @throws \Shopware\Components\Api\Exception\ParameterMissingException * * @return PaymentModel */
$manufacturer->fromArray($params);

        if (isset($params['id'])) {
            $metaData = $this->getManager()->getMetadataFactory()->getMetadataFor(ManufacturerModel::class);
            $metaData->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
            $manufacturer->setPrimaryIdentifier($params['id']);
        }

        $violations = $this->getManager()->validate($manufacturer);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($manufacturer);
        $this->flush();

        return $manufacturer;
    }

    /** * @param int $id * * @throws NotFoundException * @throws ParameterMissingException * @throws ValidationException * * @return ManufacturerModel */
public function create(array $data$index = false)
    {
        $this->checkPrivilege('save');

        $data = $this->prepareData($data);

        $stream = new CustomerStreamEntity();
        $stream->fromArray($data);

        $violations = $this->getManager()->validate($stream);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->validateStream($stream);

        $this->getManager()->persist($stream);
        $this->getManager()->flush($stream);

        if ($index) {
            $this->indexStream($stream);
        }

        
public function create(array $params)
    {
        $this->checkPrivilege('create');

        $params = $this->prepareCountryData($params);

        $country = new CountryModel();
        $country->fromArray($params);

        $violations = $this->getManager()->validate($country);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($country);
        $this->flush();

        return $country;
    }

    /** * Updates the Country entity with the given ID using the passed params. * * @param int $id * * @throws NotFoundException * @throws ValidationException * @throws ParameterMissingException * * @return CountryModel */
if (!empty($params['translations'])) {
            $translations = $params['translations'];
            unset($params['translations']);
        }

        $params = $this->prepareAssociatedData($params$product);

        $product->fromArray($params);

        $violations = $this->getManager()->validate($product);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($product);
        $this->flush();

        if (!empty($translations)) {
            $this->writeTranslations($product->getId()$translations);
        }

        return $product;
    }

    
public function create(array $params)
    {
        $this->checkPrivilege('create');

        $params = $this->prepareShopData($params);

        $shop = new ShopModel();
        $shop->fromArray($params);

        $violations = $this->getManager()->validate($shop);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($shop);
        $this->flush();

        return $shop;
    }

    /** * @param int $id * * @throws NotFoundException * @throws ParameterMissingException * @throws ValidationException * * @return ShopModel */
if (isset($params['attribute']) && \is_array($params['attribute'])) {
            $attribute->fromArray($params['attribute']);
        }
        $media->setAttribute($attribute);

        $path = $this->prepareFilePath($media->getPath()$media->getFileName());
        $media->setPath($path);

        $violations = $this->getManager()->validate($media);
        if ($violations->count() > 0) {
            throw new ValidationException($violations);
        }

        $this->getManager()->persist($attribute);
        $this->getManager()->persist($media);
        $this->flush();

        if ($media->getType() === MediaModel::TYPE_IMAGE) {
            /** @var Manager $manager */
            $manager = $this->getContainer()->get(Manager::class);

            $manager->createMediaThumbnail($media[], true);
        }


        if ($customerType === Customer::CUSTOMER_TYPE_BUSINESS) {
            $this->validateField('company', $address->getCompany()[new NotBlank()]);

            if ($this->config->offsetGet('vatcheckrequired')) {
                $this->validateField('vatId', $address->getVatId()[new NotBlank()]);
            }
        }

        if ($this->validationContext && $this->validationContext->getViolations()->count()) {
            throw new ValidationException($this->validationContext->getViolations());
        }
    }

    /** * {@inheritdoc} */
    public function isValid(Address $address)
    {
        try {
            $this->validate($address);
        } catch (ValidationException $ex) {
            
Home | Imprint | This part of the site doesn't use cookies.