I’m trying to make a custom customer register form
And to do so, i’ve to override this 2 file:
CustomerBundle\Layout\DataProvider\FrontendCustomerUserRegistrationFormProvider.php
and
CustomerBundle\Form\Type\FrontendCustomerUserRegistrationType.php
So i’ve put this code in config/config.yml and it work as expected:
services:
oro_customer.provider.frontend_customer_user_registration_form:
class: 'My_Code\Bundle\CustomerBundle\Layout\DataProvider\FrontendCustomerUserRegistrationFormProvider'
arguments:
- "@form.factory"
- "@doctrine"
- "@oro_config.manager"
- "@oro_website.manager"
- "@oro_user.manager"
- '@router'
tags:
- { name: layout.data_provider, alias: oro_customer_frontend_customer_user_register }
oro_customer.form.type.frontend.customer_user.register:
class: 'My_Code\Bundle\CustomerBundle\Form\Type\FrontendCustomerUserRegistrationType'
arguments:
- '@oro_config.manager'
- '@oro_user.manager'
calls:
- [setDataClass, ['%oro_customer.entity.customer_user.class%']]
tags:
- { name: form.type, alias: oro_customer_frontend_customer_user_register }
But i also need to override entity as well, so it will render a new column to the customer register form.
I’ve notice that FrontendCustomerUserRegistrationType have this code:
calls:
- [setDataClass, ['%oro_customer.entity.customer_user.class%']]
And so CustomerBundle\Resources\config\services.yml have that too:
parameters:
oro_customer.entity.customer_user.class: Oro\Bundle\CustomerBundle\Entity\CustomerUser
So i’ve override it using config/config.yml:
parameters:
oro_customer.entity.customer_user.class: My_Code\Bundle\CustomerBundle\Entity\CustomerUser
But it doesn’t work, it throw an error:
The form’s view data is expected to be an instance of class My_Code\Bundle\CustomerBundle\Entity\CustomerUser, but is an instance of class Oro\Bundle\CustomerBundle\Entity\CustomerUser. You can avoid this error by setting the “data_class” option to null or by adding a view transformer that transforms an instance of class Oro\Bundle\CustomerBundle\Entity\CustomerUser to an instance of My_Code\Bundle\CustomerBundle\Entity\CustomerUser.
I think it make the override of FrontendCustomerUserRegistrationType go wrong and failed.
Here is my overrided FrontendCustomerUserRegistrationType.php:
<?php
namespace My_Code\Bundle\CustomerBundle\Form\Type;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\UserBundle\Entity\User;
use Oro\Bundle\UserBundle\Entity\UserManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Oro\Bundle\CustomerBundle\Form\Type\FrontendCustomerUserRegistrationType as OroFrontendCustomerUserRegistrationType;
class FrontendCustomerUserRegistrationType extends OroFrontendCustomerUserRegistrationType
{
/**
* @var ConfigManager
*/
protected $configManager;
/**
* @param ConfigManager $configManager
* @param UserManager $userManager
*/
public function __construct(ConfigManager $configManager, UserManager $userManager)
{
parent::__construct($configManager, $userManager);
$this->configManager = $configManager;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->isCompanyNameFieldEnabled()) {
$builder->add(
'companyName',
TextType::class,
[
'required' => true,
'mapped' => false,
'label' => 'oro.customer.customeruser.profile.company_name',
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['max' => 255])
],
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.company_name']
]
);
}
$builder
->add(
'firstName',
TextType::class,
[
'required' => true,
'label' => 'oro.customer.customeruser.first_name.label',
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.first_name']
]
)
->add(
'lastName',
TextType::class,
[
'required' => true,
'label' => 'oro.customer.customeruser.last_name.label',
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.last_name']
]
)
->add(
'email',
EmailType::class,
[
'required' => true,
'label' => 'oro.customer.customeruser.email.label',
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.email']
]
)
->add(
'test',
TextType::class,
[
'required' => true,
'label' => 'Test Label',
'attr' => ['placeholder' => 'Test Place Holder']
]
)
->add(
'test2',
TextType::class,
[
'required' => true,
'label' => 'Test2 Label',
'attr' => ['placeholder' => 'Test2 Place Holder']
]
)
;
$builder->add(
'plainPassword',
RepeatedType::class,
[
'type' => PasswordType::class,
'first_options' => [
'label' => 'oro.customer.customeruser.password.label',
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.password']
],
'second_options' => [
'label' => 'oro.customer.customeruser.password_confirmation.label',
'attr' => ['placeholder' => 'oro.customer.customeruser.placeholder.password_confirmation']
],
'invalid_message' => 'oro.customer.message.password_mismatch',
'required' => true,
'validation_groups' => ['create']
]
);
$this->addBuilderListeners($builder);
}
/**
* @return bool
*/
private function isCompanyNameFieldEnabled()
{
return (bool) $this->configManager->get('oro_customer.company_name_field_enabled');
}
}
Oh, and i’ve also created success 2 column “test” and “test2” in table “oro_customer_user” using oro:migrations.
Just ask when you need more file to check, thanks for helping :)
You also need to override the
configureOptions
method with yourdata_class
: