3

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 .

 $builder
        ->add('questionType', 'entity', array(
              'class'    => 'QuizmooQuestionnaireBundle:QuestionType',
              'property' => 'questionTypeName',
              'multiple' => false,
              'label' => 'Question Type'))
        ->add('type', 'hidden')
    ;

What am not able to achieve is to set a default value to the resulted select.

I have googled a lot but I got only preferred_choice solution which works only with arrays

hakre
  • 193,403
  • 52
  • 435
  • 836
zizoujab
  • 7,603
  • 8
  • 41
  • 72

5 Answers5

4

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 )

zizoujab
  • 7,603
  • 8
  • 41
  • 72
2
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));
Ghassan Idriss
  • 2,120
  • 20
  • 16
2

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.

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • Could you give more explanation please because as I mentioned I have already seen preferred choices docs but got nothing – zizoujab Apr 03 '13 at 10:02
  • Are you putting the value of the preferred choice or the text that is displayed? From what I can see it would be the id, if that's any help. – qooplmao Apr 03 '13 at 10:13
1

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
        ));
    }
}
Ian Phillips
  • 2,027
  • 1
  • 19
  • 31
0

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

/**
 * default the numOfCourses to 10
 *
 * @var integer
 */
private $numCourses = 10;
crmpicco
  • 16,605
  • 26
  • 134
  • 210