Symfony - Override ProfileForm - option 'class' not exists

跟風遠走 提交于 2020-01-07 02:58:05

问题


I trying to override FOSUserBundle edit form for profile, so I do somethink like this:
config.yml

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: crmBundle\Entity\User
    profile:
        form:
            type: crmBundle\Form\ProfileFormType

services.yml

services:
    app.form.profile:
        class: crmBundle\Form\ProfileFormType
        tags:
            - { name: form.type, alias: crm_user_profile }

ProfileFormType.php

namespace crmBundle\Form;

use FOS\UserBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;

class ProfileFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->buildUserForm($builder, $options);

        $builder->add('current_password', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'), array(
            'label' => 'form.current_password',
            'translation_domain' => 'FOSUserBundle',
            'class' => 'form-control', /// ????????????????
            'mapped' => false,
            'constraints' => new UserPassword(),
        ));
    }

    // BC for SF < 3.0
    public function getName()
    {
        return $this->getBlockPrefix();
    }

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

    protected function buildUserForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', null, array('label' => 'form.firstname', 'translation_domain' => 'FOSUserBundle'))
        ;
    }
}

But I getting error: The option "class" does not exists.
Here is stack trace: http://pastebin.com/CE7dzi8s
Where is problem?

EDIT: I fixed problem with class, but now I getting error:

Cannot read index "firstname" from object of type "crmBundle\Entity\User" because it doesn't implement \ArrayAccess. 

Here is my Entity/User

// src/crmBundle/Entity/User.php

namespace crmBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $firstname;

        public function getFirstname() {

            return $this->firstname;
        }

        public function setFirstname( $firstname ) {
            $this->firstname = $firstname;

            return $this;
        }

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $surname;

        public function getSurname() {

            return $this->surname;
        }

        public function setSurname( $surname ) {
            $this->surname = $surname;

            return $this;
        }

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $phone;

        public function getPhone() {

            return $this->phone;
        }

        public function setPhone( $phone ) {
            $this->phone = $phone;

            return $this;
        }

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $city;

        public function getCity() {

            return $this->city;
        }

        public function setCity( $city ) {
            $this->city = $city;

            return $this;
        }

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $ranks;

        public function getRanks() {

            return $this->ranks;
        }

        public function setRanks( $ranks ) {
            $this->ranks = $ranks;

            return $this;
        }

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

回答1:


I had the same error and adding getParent() method to the ProfileFormType class solved the issue.

....

class ProfileFormType extends AbstractType
{
    ....

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\ProfileFormType';
        // Or for Symfony < 2.8
        // return 'fos_user_registration';
    }

    ....
}

The non-required form fields inherited from it's parent (if there are) could then be removed using with ->remove() notation.

Example:

$builder->remove('email');




回答2:


Use

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

And need to specify data_class option for form

public function setDefaultOptions( OptionsResolverInterface $resolver )
{
   $resolver->setDefaults( array(
      'data_class' => 'crmBundle\Entity\User',
      'intention'  => 'profile',
   ));
}


来源:https://stackoverflow.com/questions/38416124/symfony-override-profileform-option-class-not-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!