I need to set the value of a symfony2 form element. I use a doctrine2 entity, a Symfony\\Component\\Form\\AbstractType and the createForm() method inside my
If you try fill file upload field - it's wrong idea. Ask yourself: what data should be there? If an user upload an image, then in this field will be a path to the image on his local system. But then after file is uploaded you change its name and location, so you don't need info about this path and you just don't keep it.
The appropriate way should be left empty upload field below (or above, or wherever you have it ;) ) image. Then after form is submitted and if the field is not empty you should change edited image.
For a more exact answer you should include the entities you use in this form so we can see the getters and setters. But based on your question this should work: Inside the controller do this:
$saleDataForm->getData()->getImage()->setValue('someimage.jpg');
$form->setData($form->getData());
This is if the form is already created so:
$saleDataForm = $this->createForm(new SaleType(), $sale);
$saleDataForm->getData()->getImage()->setValue('someimage.jpg');
$form->setData($form->getData());
To get the data use this:
$saleDataForm->getData()->getImage()->getValue();
thanks MatsRietdijk, you helped me, but I had to change code to this
$form = $this->createForm(new SaleType(), $sale);
$form->getData()->setImage('someimage.jpg');
$form->setData($form->getData());