0

I would like to use my UserType form class to register a user and to edit the user profile. As I want to the admins to register a user while setting up their roles, I don't want the user to modify that options. So I would like to use 2 different forms but for the same type User.

Can I create 2 different buildForm() functions in one FormType class ? Or do I need to create another type ?

Here is my UserType class (main purpose was to register a user) :

<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', TextType::class)
            ->add('lastname', TextType::class)
            ->add('birthdate', BirthdayType::class, array(
                'placeholder' => '-'))
            ->add('email', EmailType::class)
            ->add('username', TextType::class)
            ->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class,
                'first_options'  => array('label' => 'password'),
                'second_options' => array('label' => 'repeat-password'),
            ))
            ->add('roles', ChoiceType::class, [
               'multiple' => true,
               'expanded' => true, // render check-boxes
               'choices' => [
                   'Administrateur' => 'ROLE_ADMIN',
                   'Direction' => 'ROLE_MANAGER',
                   'Comptabilite' => 'ROLE_ACCOUNTING',
                   'Commercial' => 'ROLE_MARKETING',
                   'Docteur' => 'ROLE_DOCTOR',
                   'Client' => 'ROLE_CLIENT',
               ],
           ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => User::class,
        ));
    }
}
0

2 Answers 2

1

You can use a variable to dynamically add the roles to the formType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $this->is_admin = $options['is_admin'];
    // [...]
    if ($this->is_admin)
        $builder
           ->add('roles', ChoiceType::class, [
               'multiple' => true,
               'expanded' => true, 
               'choices' => [
                   'Administrateur' => 'ROLE_ADMIN',
                   'Direction' => 'ROLE_MANAGER',
                   'Comptabilite' => 'ROLE_ACCOUNTING',
                   'Commercial' => 'ROLE_MARKETING',
                   'Docteur' => 'ROLE_DOCTOR',
                   'Client' => 'ROLE_CLIENT',
               ],
           ])
        // [...]
    ;
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        // [...]
        'is_admin' => false,
    ]);
}

Then pass the variable like

$form = $this->createForm(UserType::class, $user, array(
    'is_admin' => $this->isGranted('ROLE_ADMIN'),
);
Sign up to request clarification or add additional context in comments.

1 Comment

I use directly if ($options['is_admin']) but thanks good answer :)
0

you can easily handle this using array $options
in controller set choices of roles and add them to $option
and in userType do something like this:

->add('roles', ChoiceType::class, [
           'multiple' => true,
           'expanded' => true, // render check-boxes
           'choices' => $options['choices'],
       ]) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.