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

5 Answers 5

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 )

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Could you give more explanation please because as I mentioned I have already seen preferred choices docs but got nothing
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.
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));

2 Comments

thanks for the answer, the problem is that 'foo' has to come form the database
I updated the answer, but your solution seems like the better way to go.
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
        ));
    }
}

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.