0

I create a form when calling this route:

/**
     * @Route("/product/{id}.html")
     * @Template()
     */
public function indexAction($id) {

$form = $this->createForm(new AssessmentType());

return array(
    'bewertungform' => $form->createView(),
    'angebot' => $this->loadAngebot($id)
);
}

Then I want to set the value of a field from this form with the $id. Can I do this in this Action for Example like something $form->setValue('productid',$id)?

Here is my AssessmentType Class:

..../**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
    ->add('stars', 'choice', array('choices' => array('1' => '1 star', '2' => '2 stars', '3' => '3 stars', '4' => '4 stars', '5' => '5 stars')))
    ->add('comment')
    ->add('productid')
;
}....
Zwen2012
  • 3,360
  • 9
  • 40
  • 67

1 Answers1

1

If productid field is entity type then you must pass entity object as data of this field, not its id.

So your action should look like this:

/**
 * @Route("/product/{id}.html")
 * @Template()
 */
public function indexAction($id) {
    $angebot = $this->loadAngebot($id);
    $assessment = new Assessment();
    $assessment->setProduct(angebot);  // method that sets `productid` value

    $form = $this->createForm(new AssessmentType(), $assessment);

    return array(
        'bewertungform' => $form->createView(),
        'angebot' => $angebot
    );
}
  • Thanks kibao! I resolved it by doing this in my AssesmentType Class: protected $angebotid; public function __construct(array $id = null) { $this->productid = $id['id']; } and then ->add('productid', 'text', array( 'label' => 'productid', 'data' => $this->productid )) – Zwen2012 Feb 19 '14 at 11:46