简体   繁体   中英

Show specific form on the basis of user role?

My question/problem:

Alright, so as you can probably see I use FOSUserBundle and I have 2 user roles, daily and weekly. In my registration form, I have implemented a ChoiceType to choose between them both. Right now it's not doing very much. It just puts the TYPES in my FormType and in the database when the user is created, poorly as a string and not as a proper role.

My goal is this:

After the user has chosen his role (daily or weekly) it goes to the database. Now the tricky part. I want to have specific forms shown by the users' role as you can see -> {% if is_granted('ROLE_DAILY') %} and so on ... how can I archive that with the role of the user?

Short steps:

  1. Go to Registration form
  2. Choose role (daily, weekly)
  3. Registration submit
  4. Go to XY page where the form is displayed
  5. Show user roles form (daily or weekly)

I have really NO idea how to put the role in my database and then define the specific form...

This is my security.yml:

security:
    .....
    role_hierarchy:
            ROLE_DAILY: [ROLE_DAILY]
            ROLE_WEEKLY: [ROLE_WEEKLY]

This is my form (twig):

            {% if is_granted('ROLE_DAILY') %}
                {# Show when user is role_daily #}
                {{ form_start(form) }}
                {{ form_widget(form) }}
                {{ form_end(form) }}
            {% endif %}

            {% if is_granted('ROLE_WEEKLY') %}
                {# Show when user is role_weekly #}
                {{ form_start(form) }}
                {{ form_widget(form) }}
                {{ form_end(form) }}
            {% endif %}

This is my RegistrationFormType:

class RegistrationFormType extends AbstractType
{

    const TYPES = ['Daily' => 'Daily', 'Weekly' => 'Weekly'];

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'type',
            ChoiceType::class,
            [
                'label' => false,
                'required' => true,
                'multiple' => false,
                'choices' => self::TYPES,
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

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

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

This is my registration_content.html.twig:

       <div class="form-group">
            {{ form_row(form.type, { 'attr': {'class': 'form-control choice-rhythmus'} }) }}
        </div>

This is my user entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    /**
     * @ORM\Column(type="string")
     */
    protected $first_name;
    /**
     * @ORM\Column(type="string")
     */
    protected $last_name;
    /**
     * @ORM\Column(type="string")
     */
    protected $company_name;
    /**
     * @ORM\Column(type="string")
     */
    protected $company_address;
    /**
     * @ORM\Column(type="string")
     */
    protected $company_city;
    /**
     * @ORM\Column(type="string")
     */
    protected $company_code;

    /**
     * @ORM\Column(type="string")
     */
    protected $company_business;

    /**
     * @ORM\Column(type="string", name="type", nullable=false)
     */
    protected $type;

    /**
     * @return mixed
     */
    public function getCompanyBusiness()
    {
        return $this->company_business;
    }

    /**
     * @param mixed $company_business
     */
    public function setCompanyBusiness($company_business)
    {
        $this->company_business = $company_business;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getCompanyName()
    {
        return $this->company_name;
    }

    /**
     * @param mixed $company_name
     */
    public function setCompanyName($company_name)
    {
        $this->company_name = $company_name;
    }

    /**
     * @return mixed
     */
    public function getCompanyAddress()
    {
        return $this->company_address;
    }

    /**
     * @param mixed $company_address
     */
    public function setCompanyAddress($company_address)
    {
        $this->company_address = $company_address;
    }

    /**
     * @return mixed
     */
    public function getCompanyCity()
    {
        return $this->company_city;
    }

    /**
     * @param mixed $company_city
     */
    public function setCompanyCity($company_city)
    {
        $this->company_city = $company_city;
    }

    /**
     * @return mixed
     */
    public function getCompanyCode()
    {
        return $this->company_code;
    }

    /**
     * @param mixed $company_code
     */
    public function setCompanyCode($company_code)
    {
        $this->company_code = $company_code;
    }

    /**
     * @return mixed
     */
    public function getFirstName()
    {
        return $this->first_name;
    }

    /**
     * @param mixed $first_name
     */
    public function setFirstName($first_name)
    {
        $this->first_name = $first_name;
    }

    /**
     * @return mixed
     */
    public function getLastName()
    {
        return $this->last_name;
    }

    /**
     * @param mixed $last_name
     */
    public function setLastName($last_name)
    {
        $this->last_name = $last_name;
    }
}

哇,我终于解决了我的问题...我只需要添加'multiple'= true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM