0

I'm creating a form with fields based on an entity,I tried to set the default values using the data option,and preferred choices following the answers to the questions asked here and here,I tried injecting entity manager from the controller and fetching an entity in the form type,I also tried injecting the entity directly ... still doesn't work. i wonder if the problem is caused by the query_builder i'm passing into the option when creating the field ( I used it to avoid duplicates as group_by doesn't do the trick). here the code of the form : To see the whole class :
http://pastebin.com/sx4du0Eb

An example of a filed :

                ->add('quantite', 'entity', array(
                'class' => 'CcWebBundle:Brochure',
                'property' => 'quantite',
                'multiple' => false,
                // utilisation d'une requête pour filtrer les resultats
                'query_builder' => function(EntityRepository $er) {
                                    return $er->createQueryBuilder('u')
                                              ->groupBy('u.quantite');
                                    }
                  ))

1 Answer 1

0

I think the main problem is with the form itself. Are you sure that every property on your Brochure entity is an instance of Brochure?

->add('quantite', 'entity', array(
            'class' => 'CcWebBundle:Brochure',
            'property' => 'quantite',
            'multiple' => false,
            // utilisation d'une requête pour filtrer les resultats
            'query_builder' => function(EntityRepository $er) {
                                return $er->createQueryBuilder('u')
                                          ->groupBy('u.quantite');
                                }
              ))

I guess you want to set a quantity here? If so that should be a number:

->add(
    'quantite',
    'number'
)

You use an entity field type if you want to set a relation, for example the type of the Brochure, and you have a manyToOne pointing from Brochure to BrochureType. Once you correct all your form fields, setting default values for the form should be as simple as:

public function newAction()
{
    $brochure = new Brochure();
    $brochure->setQuantite(5);
    $form = $this->createForm(new BrochureType(), $brochure);
    // ..
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your reply, but the quantity and all the fields are all fetched from the databse and added to the form to be shown as a select, this way the form displays all the possibles choices.
By setting the form type 'number' to the quantity field it does not mean it won't be fetched from the database, it just means that the rendered form element will be an <input type="number"> instead of a <select>. Can you post your Brochure.orm.yml or Entity/Brochure.php
pastebin.com/YCW8d8x3, if i change the setting ( the string fields) will all options be automatically loaded into the select like it was loaded as an entity ?
All fields on the Brochure entity seems to be plain string. Do you want the user to enter the values via select boxes on the form for these fields? If so where do these values should come from?
indeed they are all strings but the brochure entity enherites from a supermapped class that contains other fields. yes i want the user to input values via select boxes,the values come from correspondant fields of brochure instances stored in the database by doctrine .

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.