Hi, I’m trying to do following:
Adding a new field (checkbox named companyConfirmation) to the billing address, the new field should only be mandatory if the country from outside the European Union is selected from the country select. The new field should not appear in the shipping address section. In addition, the form should be able to be sent only if the customer_type field is equal to “business”.
From a technical point of view, I tried to solve it in the following way:
I tried to modify the registration form. I used the documentation Shopware: https://developers.shopware.com/developers-guide/address-management-guide/#example-#1:-adding-a-new-field but in this case it will not work because I need to modify addVatIdValidation and private method to add another method of validating a new checkbox on the form (checkbox companyConfirmation).
In addition, the controller would need to pass in options (public function buildForm(FormBuilderInterface $builder, array $options)) 3 variables: isShippingProvided, AddressType and companyConfirmation.
I tried to do it in the following way:
public static function getSubscribedEvents()
{
return [
'Shopware_Form_Builder' => 'onAddressFormBuild',
];
}
public function onAddressFormBuild(\Enlight_Event_EventArgs $event)
{
if ($event->getReference() !== BaseAddressFormType::class) {
return;
}
$builder = $event->getBuilder();
$formBuilder = $builder->getConfig();
$this->addVatIdValidation($formBuilder);
if ($this->addressType == self::ADDRESS_TYPE_BILLING) {
$this->addCompanyConfirmationValidation($builder);
}
}
/**
* @param FormBuilderInterface $builder
*/
private function addVatIdValidation(FormBuilderInterface $builder)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$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->getVatId())) {
return;
}
if ($this->addressType == self::ADDRESS_TYPE_SHIPPING && !$this->isShippingProvided) {
return;
}
if (EUStates::isEUCountry($data->getCountry()->getIso())) {
$notBlank = new NotBlank(['message' => null]);
$error = new FormError($notBlank->message);
$error->setOrigin($form->get('vatId'));
$form->addError($error);
}
});
}
/**
* @param FormBuilderInterface $builder
*/
private function addCompanyConfirmationValidation(FormBuilderInterface $builder)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
/** @var Address $data */
$data = $form->getData();
if (!$this->companyConfirmation && !EUStates::isEUCountry($data->getCountry()->getIso())) {
$message = 'Please confirm you place your order for a business entity by checking the box below. The products on this site are available for commercial customers only.';
$error = new FormError($message);
$error->setOrigin($form->get('additional')->get('companyConfirmation'));
$form->addError($error);
}
});
}
but an error appeard:
“FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance. in vendor/symfony/form/FormConfigBuilder.php on line 211”
Thanks in advance.