Symfony-覆盖ProfileForm-选项'class'不存在 [英] Symfony - Override ProfileForm - option 'class' not exists

查看:72
本文介绍了Symfony-覆盖ProfileForm-选项'class'不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖个人资料的FOSUserBundle编辑表单,所以我做这样的事情:
config.yml

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'))
        ;
    }
}

但是我收到错误消息:选项"class"不存在.
这是堆栈跟踪: http://pastebin.com/CE7dzi8s
问题出在哪里?

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

我解决了班级问题,但现在出现错误:

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. 

这是我的实体/用户

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

推荐答案

我遇到了相同的错误,将getParent()方法添加到ProfileFormType类中解决了该问题.

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';
    }

    ....
}

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

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

示例:

$ builder->删除('email');

$builder->remove('email');

这篇关于Symfony-覆盖ProfileForm-选项'class'不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆