donglu5235 2017-06-07 12:06
浏览 45


I have a form builder which builds a form

public function buildForm(FormBuilderInterface $builder, array $options)
        add('typeTask',TextType::class,array('label'=>"Вид заявка"))->
        add('term',DateType::class, array(
            'widget' => 'choice',
            'label'=>"Краен срок"
        add('executioner',TextareaType::class,array('label'=>"Под изпълнител",

As you see I have 2 fields which are "approved" which can be true or false and rejected which can also be true and false. Usually they are hidden because only 1 type of user can access them - ROLE_ADMIN and the rest is for ROLE_EDITOR. In my case the ADMIN needs to only approve or reject it and the EDITOR can't do that. The biggest issue is that I don't need a whole form, but rather 2 buttons - "Approve" and "Reject" when the Project is shown ("show" action), but the action which changes the Project is "edit" and so what I tried so far is from "show" to send a form to "edit" and then when the edit action is over to load the "show" action again.I tried achieving this by creating 2 forms - approveForm and rejectForm which can hold only 1 property each and send and flush them to "edit" function, but the edit function doesn't accept the form and also if it did it would have deleted everything else. Here is my code so far

In show action -

$projectFormApprove = $this->createForm('AppBundle\Form\ProjectType', $project,array(
        $projectFormReject = $projectFormApprove;

This will create 2 forms each having 1 property and here is what happens in my twig template

           {{ form_start(approveForm, {'action': path('project_edit', { 'id': })}) }}
           {{ form_widget(approveForm) }}
               <input type="submit" value="Approve" />
           {{ form_end(approveForm) }}
        {{ form_start(rejectedForm,{'action': path('project_edit', { 'id': })}) }}
               {{ form_widget(rejectedForm) }}
                 <input type="submit" value="Reject" />
               {{ form_end(rejectedForm) }}

I need two forms since there are 2 buttons which simply submit them and no one actually changes the value ( this is the reason why in "show" function the created property have "data"=>true. If the form is submitted it will do it automatically. Here is what is in my "edit" function -

/** @var  $user User */
        $user = $this->getUser();
        $project = new Project();
        $form = $this->createForm('AppBundle\Form\ProjectType', $project);
        if($user->getType() != "LittleBoss"){

        if ($form->isSubmitted() && $form->isValid()) {
        $project->setDate(new \DateTime());
            $em = $this->getDoctrine()->getManager();

            return $this->redirectToRoute('project_show', array('id' => $project->getId()));

        return $this->render('project/new.html.twig', array(
            'project' => $project,
            'form' => $form->createView(),

Now to my actual problem - As you see I first remove "approved" field and then I add new one with predefined value. What I want is to change not the values, but the type of description and the rest fields. Is there a way to say $form->change(); or anything that can change the types of the fields without having to remove them. The type I want them to be is HiddenType and set their data so that when I submit one of the 2 forms it will be accepted as valid in the "edit" action then flushed to the database and everything will be fine. So far when one of the buttons - "Approve" or "Reject" is clicked in the "edit" action $edit_form->IsSubmited() returns false.

  • 写回答

1条回答 默认 最新

  • duan0065626385 2017-06-07 13:16

    I suggest you to create seperate forms, one for editor and another for admin. Then in controller use the form you need by permissions of the logged in user.

    if ($this->authorizationChecker->isGranted('ROLE_EDITOR')) {
         $form = $this->createForm(EditorType::class);
    } elseif ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
         $form = $this->createForm(AdminType::class);

    In both forms you can use same entity, but different fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 铜与钢双金属板叠加在一起每种材料300mm长,18mm宽,4mm厚一端固定并加热至80℃,当加热端温度保持不变时另一端的稳态温度。ansys
  • ¥15 django5安装失败
  • ¥60 ios系统storm sniffer 写入http规则加密如何破解?
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥15 ANSYS分析简单钎焊问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上