Length example

return $constraints;
        }

        $configKey = self::CONFIG_MIN_LENGTH_FOR[$field->getFor()];

        $minPasswordLength = $this->configService->getInt($configKey);

        if ($minPasswordLength === 0) {
            return $constraints;
        }

        $constraints[] = new Length(['min' => $minPasswordLength]);

        return $constraints;
    }
}

        $minPasswordLength = 8;

        $notBlankConstraints = [
            new NotBlank(),
            new Type('string'),
        ];

        $minLengthConstraints = [
            new NotBlank(),
            new Type('string'),
            new Length(['min' => $minPasswordLength]),
        ];

        yield 'with null value without min length required' => [
            PasswordField::FOR_ADMIN,
            0,
            $notBlankConstraints,
            true,
            null,
        ];

        yield 'with null value with min length required' => [
            


    /** * @throws ConstraintViolationException */
    private function validateHash(DataBag $data, SalesChannelContext $context): void
    {
        $definition = new DataValidationDefinition('customer.recovery.get');

        $hashLength = 32;

        $definition->add('hash', new NotBlank()new Type('string')new Length($hashLength));

        $this->dispatchValidationEvent($definition$data$context->getContext());

        $this->validator->validate($data->all()$definition);
    }

    private function dispatchValidationEvent(DataValidationDefinition $definition, DataBag $data, Context $context): void
    {
        $validationEvent = new BuildValidationEvent($definition$data$context);
        $this->eventDispatcher->dispatch($validationEvent$validationEvent->getName());
    }

    
/** * @throws ConstraintViolationException */
    private function validatePasswordFields(DataBag $data, SalesChannelContext $context): void
    {
        $definition = new DataValidationDefinition('customer.password.update');

        $minPasswordLength = $this->systemConfigService->get('core.loginRegistration.passwordMinLength', $context->getSalesChannel()->getId());

        $definition
            ->add('newPassword', new NotBlank()new Length(['min' => $minPasswordLength])new EqualTo(['propertyPath' => 'newPasswordConfirm']))
            ->add('password', new CustomerPasswordMatches(['context' => $context]));

        $this->dispatchValidationEvent($definition$data$context->getContext());

        $this->validator->validate($data->all()$definition);

        $this->tryValidateEqualtoConstraint($data->all(), 'newPassword', $definition);
    }

    private function tryValidateEqualtoConstraint(array $data, string $field, DataValidationDefinition $validation): void
    {
        
$criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('registrationSalesChannels.id', $context->getSalesChannel()->getId()));

        $validation->add('requestedGroupId', new EntityExists([
            'entity' => 'customer_group',
            'context' => $context->getContext(),
            'criteria' => $criteria,
        ]));

        if (!$isGuest) {
            $minLength = $this->systemConfigService->get('core.loginRegistration.passwordMinLength', $context->getSalesChannelId());
            $validation->add('password', new NotBlank()new Length(['min' => $minLength]));
            $options = ['context' => $context->getContext(), 'salesChannelContext' => $context];
            $validation->add('email', new CustomerEmailUnique($options));
        }

        $validationEvent = new BuildValidationEvent($validation$data$context->getContext());
        $this->eventDispatcher->dispatch($validationEvent$validationEvent->getName());

        return $validation;
    }

    /** * @return array<string, mixed> */
ReviewFormEvent::EVENT_NAME
        );

        return new NoContentResponse();
    }

    private function validate(DataBag $data, Context $context): void
    {
        $definition = new DataValidationDefinition('product.create_rating');

        $definition->add('name', new NotBlank());
        $definition->add('title', new NotBlank()new Length(['min' => 5]));
        $definition->add('content', new NotBlank()new Length(['min' => 40]));

        $definition->add('points', new GreaterThanOrEqual(1)new LessThanOrEqual(5));

        if ($data->get('id')) {
            $criteria = new Criteria();
            $criteria->addFilter(new EqualsFilter('customerId', $data->get('customerId')));
            $criteria->addFilter(new EqualsFilter('id', $data->get('id')));

            $definition->add('id', new EntityExists([
                'entity' => 'product_review',
                


    /** * @throws ConstraintViolationException */
    private function validateResetPassword(DataBag $data, SalesChannelContext $context): void
    {
        $definition = new DataValidationDefinition('customer.password.update');

        $minPasswordLength = $this->systemConfigService->get('core.loginRegistration.passwordMinLength', $context->getSalesChannel()->getId());

        $definition->add('newPassword', new NotBlank()new Length(['min' => $minPasswordLength])new EqualTo(['propertyPath' => 'newPasswordConfirm']));

        $this->dispatchValidationEvent($definition$data$context->getContext());

        $this->validator->validate($data->all()$definition);

        $this->tryValidateEqualtoConstraint($data->all(), 'newPassword', $definition);
    }

    private function dispatchValidationEvent(DataValidationDefinition $definition, DataBag $data, Context $context): void
    {
        $validationEvent = new BuildValidationEvent($definition$data$context);
        


    public function isFloat(): self
    {
        $this->addConstraint(new Type('numeric'));

        return $this;
    }

    public function isLengthLessThanOrEqual(int $maxLength): self
    {
        $this->addConstraint(new Length(['max' => $maxLength]));

        return $this;
    }

    public function isGreaterThanOrEqual(int $value): self
    {
        $this->addConstraint(new GreaterThanOrEqual($value));

        return $this;
    }

    
'expected' => [],
        ];

        yield 'element config with type string' => [
            'elementConfig' => [
                'required' => true,
                'dataType' => 'string',
                'minLength' => 1,
                'maxLength' => 255,
            ],
            'expected' => [
                new Assert\Length(['min' => 1]),
                new Assert\Length(['max' => 255]),
                new Assert\Type('string'),
                new Assert\NotBlank(),
            ],
        ];

        yield 'element config with type int' => [
            'elementConfig' => [
                'required' => true,
                'dataType' => 'int',
                'min' => 1,
                


    /** * @param array<string, mixed> $elementConfig * * @return array<int, Constraint> */
    private function buildConstraintsWithConfigs(array $elementConfig): array
    {
        /** @var array<string, callable(mixed): Constraint> $constraints */
        $constraints = [
            'minLength' => fn (mixed $ruleValue) => new Assert\Length(['min' => $ruleValue]),
            'maxLength' => fn (mixed $ruleValue) => new Assert\Length(['max' => $ruleValue]),
            'min' => fn (mixed $ruleValue) => new Assert\Range(['min' => $ruleValue]),
            'max' => fn (mixed $ruleValue) => new Assert\Range(['max' => $ruleValue]),
            'dataType' => fn (mixed $ruleValue) => new Assert\Type($ruleValue),
            'required' => fn (mixed $ruleValue) => new Assert\NotBlank(),
        ];

        $constraintsResult = [];

        foreach ($constraints as $ruleName => $constraint) {
            if (!\array_key_exists($ruleName$elementConfig)) {
                


    /** * @param StringField $field * * @return Constraint[] */
    protected function getConstraints(Field $field): array
    {
        $constraints = [
            new Type('string'),
            new Length(['max' => $field->getMaxLength()]),
        ];

        if (!$field->is(AllowEmptyString::class)) {
            $constraints[] = new NotBlank();
        }

        if ($field->is(AllowEmptyString::class) && $field->is(Required::class)) {
            $constraints[] = new NotNull();
        }

        return $constraints;
    }
Home | Imprint | This part of the site doesn't use cookies.