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 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题