问题描述
我试图覆盖个人资料的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 }
profodformtype.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')) ; } }
但是我遇到错误:选项"类"不存在.
这是堆栈跟踪: 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 } }
推荐答案
i存在相同的错误,并将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() note法删除从其父的继承(如果有)继承的非频率表单字段.
示例:
$ builder-> remove('email');
其他推荐答案
使用
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
并且需要指定form
的data_class选项public function setDefaultOptions( OptionsResolverInterface $resolver ) { $resolver->setDefaults( array( 'data_class' => 'crmBundle\Entity\User', 'intention' => 'profile', )); }
问题描述
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 } }
推荐答案
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');
其他推荐答案
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', )); }