Set Default value of choice field Symfony FormType

后端 未结 5 1881
野性不改
野性不改 2021-01-21 03:12

I want from the user to select a type of questionnaire, so I set a select that contains questionnaires types.

Types are loaded from a an entity QuestionType

相关标签:
5条回答
  • 2021-01-21 03:26

    If you are using the entity results to create a select menu then you can use preferred_choices.

    The preferred choice(s) will be rendered at the top of the list as it says on the docs and so the first will technically be the default providing you don't add an empty value.

    0 讨论(0)
  • 2021-01-21 03:27
    class MyFormType extends AbstractType{
    
            public function __construct($foo){
              $this->foo = $foo;
            }
    
    
            $builder
                ->add('questionType', 'entity', array(
                      'class'    => 'QuizmooQuestionnaireBundle:QuestionType',
                      'property' => 'questionTypeName',
                      'multiple' => false,
                      'label' => 'Question Type'
    
                      'data' => $this->foo))
    
                ->add('type', 'hidden')
            ;
    }
    

    In controller

    $this->createForm(new MyFormType($foo));
    
    0 讨论(0)
  • 2021-01-21 03:38

    The accepted answer of setting in the model beforehand is a good one. However, I had a situation where I needed a default value for a certain field of each object in a collection type. The collection has the allow_add and allow_remove options enabled, so I can't pre-instantiate the values in the collection because I don't know how many objects the client will request. So I used the empty_data option with the primary key of the desired default object, like so:

    class MyChildType
    extends AbstractType 
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('optionalField', 'entity', array(
                'class' => 'MyBundle:MyEntity',
                // Symfony appears to convert this ID into the entity correctly!
                'empty_data' => MyEntity::DEFAULT_ID,
                'required' => false,
            ));
        }
    }
    
    class MyParentType
    extends AbstractType 
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('children', 'collection', array(
                'type' => new MyChildType(),
                'allow_add' => true
                'allow_delete' => true,
                'prototype' => true,  // client can add as many as it wants
            ));
        }
    }
    
    0 讨论(0)
  • 2021-01-21 03:43

    I made it by setting a type in the newAction of my Controller I will get the seted type as default value.

    public function newAction($id)
    {
        $entity = new RankingQuestion();
    
        //getting values form database 
        $em = $this->getDoctrine()->getManager();
        $type =  $em->getRepository('QuizmooQuestionnaireBundle:QuestionType')->findBy(array('name'=>'Ranking Question'));
        $entity->setQuestionType($type); // <- default value is set here 
    
        // Now in this form the default value for the select input will be 'Ranking Question'
        $form   = $this->createForm(new RankingQuestionType(), $entity);
    
        return $this->render('QuizmooQuestionnaireBundle:RankingQuestion:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'id_questionnaire' =>$id
        ));
    }
    

    You can use data attribute if you have a constant default value (http://symfony.com/doc/current/reference/forms/types/form.html) but it wont be helpful if you are using the form to edit the entity ( not to create a new one )

    0 讨论(0)
  • 2021-01-21 03:52

    Set a default value on the member variable inside your entity (QuestionType), e.g.

    /**
     * default the numOfCourses to 10
     *
     * @var integer
     */
    private $numCourses = 10;
    
    0 讨论(0)
提交回复
热议问题