Submitting the user using the formFactory option

I followed this thread: FOS UserBundle - Override FormFactory

Because I want to add a field to my ProfileFormType which depends on the value of the user attribute. Since you cannot get the user from the FormType, I would like to override the formFactory to pass the current user through an array of parameters. So this is exactly what was done in the previous question.

But I get the error: "Option" user "does not exist. Known parameters are" action "," attr "," auto_initialize "," block_name "," by_reference "," cascade_validation ", etc.". So I am guessing that I made a mistake or that it could have been a typo in the service names in the previous post. So I would like you to check if you find anything suspicious in my code (IMO I'm not sure about buildForm / buildUserForm). Here are my sources, thanks!

ProfileController:

    class ProfileController extends ContainerAware
{
    public function showAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), array('user' => $user));
    }

    public function editAction(Request $request)
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->container->get('event_dispatcher');

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->container->get('cua_user.profile.form.factory');
        $formFactory->setUser($user);
        $form = $formFactory->createForm();
        $form->setData($user);

      

Services:

cua_user.profile.form.factory:
    class: Cua\UserBundle\Form\Factory\FormFactory
    arguments: ["@form.factory", "%fos_user.profile.form.name%", "%fos_user.profile.form.type%", "%fos_user.profile.form.validation_groups%"]

      

ProfileFormType:

namespace Cua\UserBundle\Form\Type;

use Cua\UserBundle\Entity;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword as OldUserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;


class ProfileFormType extends BaseType
{

    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if (class_exists('Symfony\Component\Security\Core\Validator\Constraints\UserPassword')) {
            $constraint = new UserPassword();
        } else {
            // Symfony 2.1 support with the old constraint class
            $constraint = new OldUserPassword();
        }

        $this->buildUserForm($builder, $options);

        $builder->add('current_password', 'password', array(
                'label' => 'form.current_password',
                'translation_domain' => 'FOSUserBundle',
                'mapped' => false,
                'constraints' => $constraint,
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => $this->class,
                'intention'  => 'profile',
        ));
    }

    protected function buildUserForm(FormBuilderInterface $builder, array $options)
    {

        parent::buildUserForm($builder, $options);
        $builder
            ->remove('email')
            ->add('email', 'repeated', array(
                    'type' => 'email',
                    'options' => array('translation_domain' => 'FOSUserBundle'),
                    'first_options' => array('label' => ' '),
                    'second_options' => array('label' => ' '),
                    'invalid_message' => 'Les deux adresses mail ne correspondent pas',
            ))
            ->add('pubEmail', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('nom', 'text', array('label' => 'Nom'))
            ->add('pnom', 'text', array('label' => 'Prénom'))
            ->add('annee')
            ->remove('username')
            ->add('fixe', 'text', array('label' => 'Tel. fixe', 'required'=>false))
            ->add('pubFixe', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('mobile', 'text', array('label' => 'Tel. mobile', 'required'=>false))
            ->add('pubMobile', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('facebook', 'url', array('label' => 'Adresse du profil facebook', 'required'=>false))
            ->add('pubFacebook', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('twitter', 'text', array('label' => 'Compte Twitter', 'required'=>false))
            ->add('pubTwitter', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('linkedin','url', array('label' => 'Adresse du profil Linkedin', 'required'=>false))
            ->add('pubLinkedin', 'checkbox', array('label' => ' ', 'required'=>false))
            ->add('site', 'url', array('label' => 'Site perso', 'required'=>false));
            if (!$options['user']->getStatut()==1)
            {
                $builder->add('antenne', 'entity', array('class'=>'CuaAnnuaireBundle:Groupe', 'property' => 'nom', 'multiple'=>false));
            }
    }

    public function getName()
    {
        return 'cua_user_profile';
    }


}

      

FormFactory:

    namespace Cua\UserBundle\Form\Factory;

use Symfony\Component\Form\FormFactoryInterface;
use FOS\UserBundle\Form\Factory\FactoryInterface;

class FormFactory implements FactoryInterface
{
    private $formFactory;
    private $name;
    private $type;
    private $validationGroups;
    private $user;

    public function __construct(FormFactoryInterface $formFactory, $name, $type, array $validationGroups = null)
    {
        $this->formFactory = $formFactory;
        $this->name = $name;
        $this->type = $type;
        $this->validationGroups = $validationGroups;
    }

    public function createForm()
    {
        return $this->formFactory->createNamed($this->name, $this->type, null, array('validation_groups' => $this->validationGroups, 'user'=>$this->user));
    }

    public function setUser($user)
    {
        $this->user = $user;
    }
}

      

+3


source to share


1 answer


You need to specify this parameter as needed, you can also give it a default value:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{

    $resolver->setRequired(array(
        'user'
    ));

    $resolver->setDefaults(array(
        'user'  => null,
    ));
}

      



More on Resolver here :

0


source







All Articles