How to add a registration form another field Symfo

2019-08-12 08:54发布

问题:

How to add a field on the registration form ? I do so:

\\src\UserBundle\Form\Type\RegistrationFormType.php
    <?php 
    namespace UserBundle\Form\Type;

    //use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;

    class RegistrationFormType extends BaseType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
            $builder->add('realname');
        }

        public function getParent()
        {
            return 'fos_user_registration';
        }

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

\\src\UserBundle\Resources\config 
services:
     app_user.registration.form.type:
        class: UserBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
        - { name: form.type, alias: app_user_registration }

\\src\UserBundle\Entity\User.php
namespace UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="web_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $realname;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set realname
     *
     * @param string $realname
     * @return User
     */
    public function setRealname($realname)
    {
        $this->realname = $realname;

        return $this;
    }

    /**
     * Get realname
     *
     * @return string 
     */
    public function getRealname()
    {
        return $this->realname;
    }
}

\\src\UserBundle\Resources\config\services.yml
services:
     app_user.registration.form.type:
        class: UserBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
        - { name: form.type, alias: app_user_registration }

I can not understand what is missing The result is a mistake: Could not load type "app_user_registration" Help me please!!! I think that the service does not find

回答1:

I had the same problem. (I actually found your post by googling the error we both had) I have just started learning and toying with symfony, so please take this with a grain of salt.

Try this in your config.yml :

  1. delete the service you wrote and
  2. replace it with: fos_user: db_driver: orm firewall_name: main user_class: UserBundle\Entity\User registration: form: type: app_user_registration