Symfony - 覆盖ProfileForm - 选项' class'不存在

时间:2016-07-16 22:12:08

标签: php fosuserbundle symfony

我试图覆盖配置文件的FOSUserBundle编辑表单,所以我做了一些这样的想法:
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'))
        ;
    }
}

但我收到错误:选项&#34; class&#34;不存在。
这是堆栈跟踪:http://pastebin.com/CE7dzi8s
哪里有问题?

编辑:我修复了课程问题,但现在我收到了错误:

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

这是我的实体/用户

// 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
    }
}

2 个答案:

答案 0 :(得分:2)

我遇到了同样的错误,并向getParent()类添加了ProfileFormType方法解决了这个问题。

....

class ProfileFormType extends AbstractType
{
    ....

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

    ....
}

然后可以使用->remove()表示法删除从其父级继承的非必需表单字段(如果有)。

示例:

  

$ builder-&GT;清除( '电子邮件');

答案 1 :(得分:0)

使用

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

需要为表单

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