Hesiode
Hesiode

Reputation: 53

Symfony 3 FosUserBundle edit profile

I try to edit user by administrator in backend (username, password, custom fields) :

    $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()));

But I have an error :

Warning: Missing argument 1 for AppBundle\Form\User\UserEditType::__construct()

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

Upvotes: 1

Views: 579

Answers (1)

Federkun
Federkun

Reputation: 36924

The default FOS\UserBundle\Form\Type\RegistrationFormType::__construct require a the User class name as an argument. Since you already provided it inside configureOptions, you can override the __construct of your UserEditType.

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

    // ...

Upvotes: 1

Related Questions