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');
                                    }
                  ))
Community
  • 1
  • 1
l a a b i
  • 23
  • 1
  • 9

1 Answers1

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);
    // ..
}
bagonyi
  • 3,258
  • 2
  • 22
  • 20
  • 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. – l a a b i Nov 29 '13 at 16:20
  • 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 instead of a – bagonyi Nov 29 '13 at 16:30
  • http://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 ? – l a a b i Nov 29 '13 at 16:37
  • 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? – bagonyi Nov 29 '13 at 16:45
  • 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 . – l a a b i Nov 29 '13 at 16:49