FormError example

$form = $event->getForm();

            /** @var Address $data */
            $data = $form->getData();
            $customerType = $form->get('additional')->get('customer_type')->getData();

            if ($customerType !== Customer::CUSTOMER_TYPE_BUSINESS || !empty($data->getCompany())) {
                return;
            }

            $notBlank = new NotBlank(['message' => null]);
            $error = new FormError($notBlank->message);
            $error->setOrigin($form->get('company'));
            $form->addError($error);
        });
    }

    private function addVatIdValidation(FormBuilderInterface $builder)
    {
        $builder->addEventListener(FormEvents::POST_SUBMIT, function DFormEvent $event) {
            $form = $event->getForm();

            /** @var Address $data */
            
$emailConstraint = new CustomerEmail([
            'shop' => $constraint->getShop(),
            'customerId' => $customer->getId(),
            'accountMode' => $accountMode,
        ]);

        $this->customerEmailValidator->initialize($this->context);
        $this->customerEmailValidator->validate($email$emailConstraint);

        if ($form->has('emailConfirmation') && $form->get('emailConfirmation')->getData() !== $email) {
            $error = new FormError($this->getSnippet());
            $error->setOrigin($form->get('emailConfirmation'));
            $form->addError($error);
        }
    }

    /** * @return string */
    public function validatedBy()
    {
        return 'FormEmailValidator';
    }
if ($this->isFastLogin($form)) {
            return;
        }

        $minLength = (int) $this->config->get('minPassword');

        if (empty($password) || ($minLength && \strlen($password) < $minLength)) {
            $this->addError($this->getSnippet(self::SNIPPET_PASSWORD_LENGTH));
        }

        if ($form->has('passwordConfirmation') && $form->get('passwordConfirmation')->getData() !== $password) {
            $error = new FormError($this->getSnippet(self::SNIPPET_PASSWORD_CONFIRMATION));
            $error->setOrigin($form->get('passwordConfirmation'));
            $form->addError($error);
        }
    }

    private function addError(string $message): void
    {
        $this->context->buildViolation($message)
            ->atPath($this->context->getPropertyPath())
            ->addViolation();
    }

    
Home | Imprint | This part of the site doesn't use cookies.