Symfony 3 FosUserBundle编辑个人资料

时间:2016-12-12 13:52:56

标签: php symfony

我尝试在后端通过管理员编辑用户(用户名,密码,自定义字段):

    $form = $this->createForm(UserEditType::class, $idUser, array('class' => 'AppBundle/Entity/User'));
    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);
        if ($form->isValid()) {

            $this->get('fos_user.user_manager')->updateUser($idUser);


            return $this->redirect($this->generateUrl('user_liste'));
        }
    }

    return $this->render(':User:userEdit.html.twig', array('form' => $form->createView()));

但我有一个错误:

  

警告:缺少参数1   的appbundle \表格\用户\ UserEditType :: __构建体()

我的userEdittype:

<?php

namespace AppBundle\Form\User;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;

class UserEditType extends BaseRegistrationFormType
{


    public function buildForm(FormBuilderInterface $builder, array $options)
    {


        $builder
            ->add('nom', TextType::class, array(
                    'constraints' => array(
                        new NotBlank(),
                    )
                )
            )
            ->add('prenom', TextType::class, array(
                    'constraints' => array(
                        new NotBlank(),
                    )
                )
            );

    }

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

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'AppBundle\Entity\User'));
    }
}

1 个答案:

答案 0 :(得分:0)

默认FOS\UserBundle\Form\Type\RegistrationFormType::__construct需要User类名作为参数。由于您已在configureOptions内提供,因此您可以覆盖UserEditType的__construct。

class UserEditType extends BaseRegistrationFormType
{
    public function __construct(/* nothing */) 
    {
    }

    // ...