2017-09-26 106 views
1

我的问题/问题:在用户角色的基础上显示特定的表单?

好了,所以你很可能会看到我用FOSUserBundle和我有2个用户角色,每天和每周的。在我的注册表格中,我实现了一个ChoiceType来在它们之间进行选择。现在它没有做太多。它只是在创建用户时将TYPES放入我的FormType和数据库中,作为一个字符串很糟糕,而不是一个合适的角色。

我的目标是:

用户选择他的角色(每天或每周)之后,它进入到数据库中。现在是棘手的部分。我想要具有用户角色显示的特定表单 - > {%if is_granted('ROLE_DAILY')%}等等......我如何将它与用户的角色一起归档?

简单步骤:

  1. 前往登记表
  2. 选择角色(每日,每周)
  3. 注册提交
  4. 转到xy其中的形式显示
  5. 显示页用户角色表(每日或每周)

我真的不知道如何把角色在我的数据库,然后定义的具体形式......

这是我security.yml:

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

这是我的形式(小枝):

  {% 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 %} 

这是我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(); 
    } 
} 

这是我registration_content.html.twig:

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

这是我的用户实体:

/** 
* @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; 
    } 
} 
+0

这并不回答你的问题关于节省e中的角色,但如果表单非常相似,则可以使用该表单并使用RegistrationFormType中的if语句来检查用户角色并添加相关表单字段。 – user742736

+0

@ user742736感谢您的提示,我会考虑到这一点后,我找到了我的问题的解决方案! – jglom

回答

0

哇,我终于解决了我的问题。我只需要添加'多个'=真

相关问题