简体   繁体   中英

How can I override FOSUserBundle form using my own fields(Symfony 3)?

My RegistrationType class code:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

/**
 * Форма регистрации пользователя
 */
class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('adress');
        $builder->add('mobileNumber');
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

}

It give me standart form with added fields "adress" and "mobileNumber".

Where can I choose which standart fields I want to use?

What should I change to get only "adress" and "mobileNumber" fields? Thank you!

If your question is "How to remove fields from a form" then:

Solution 1:

You can remove any field from a form with that builder method

$builder->remove('your_field_name')

Maybe you should also override the FOSUser controller to fill the mandatory fields of your user entity. It depends how you modify your user entity.

Solution 2: Using Form Options

Take a look here https://knpuniversity.com/screencast/question-answer-day/remove-form-field

There is a very good article on this topic:

Overriding Default FOSUserBundle Forms

Have you defined your forma as a service via form.type tag? Have you told FOS about your form type in services.yml (via fos_user.registration.form key)?

This is my example of code, which I write with your advices. I hope it helps someone) Thanks to all!

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

/**
 * Форма регистрации пользователя
 */
class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('address', null, [
            'label' => 'Адрес дома',
            'attr'=> [
                'placeholder' => 'placeholder-text'
                ]
            ]);
        $builder->add('mobileNumber', null, [
            'label' => 'Мобильный телефон',
            'attr'=> [
                'placeholder' => '+7 (999) 123-45-67'
                ]
        ]);
        $builder->add('username', null, [
            'label' => 'ФИО',
            'attr'=> [
                'placeholder' => 'Иванов Иван Иванович'
                ]
        ]);


        $builder->remove('email');
        $builder->remove('plainPassword');
    }

    public function getParent()
    {
//        return 'fos_user_registration';
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

    public function getName()
    {
        return $this->getBlockPrefix();
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM