Hi everybody I'm working onn a new project with Symfony and i have some difficulties with the edit views when they have an input file, when i open edit view for un product i have the name input hydrated with the name of product, the input price with the price of product but the photo input file empty :s
How can i do to hydarat this input file before the persist of the modification
My FormBuilder
class ProductEditType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name',TextType::class)
->add('price',TextType::class)
->add('Photo',PhotoType::class)
->add('enregistrer',SubmitType::class);
}
the controller
public function editProductAction($id, Request $request){
$product = new Product();
$repository=$this->getDoctrine()->getManager()->getRepository('MyProjectProductBundle:Product');
$product= $repository->find($id);
$form=$this->get('form.factory')->create(ProductEditType::class,$product);
if($request->isMethod('post') && $form->handleRequest($request)->isValid()){
$em=$this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirectToRoute('my_project_backoffice_product', array('id'=>$product->getId()));
}
return $this->render('MyProjectBackofficeBundle:Backoffice:editproduct.html.twig',array('form'=>$form->createView(),'product'=>$product));
}
PhotoType
class PhotoType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', FileType::class) ;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyProject\ProductBundle\Entity\Photo'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'myproject_productbundle_photo';
}
twig view
<div class="well">
{{ form_start(form) }}
<div class="container4_in">
<div class="gri4">
<div class="grid2">
<div class="form_elem_wrraper">
<div class="row_form">
{{ form_label(form.name, "Name ") }}
{# Génération de l'input. #}
{{ form_widget(form.name,{ 'attr': {'class' :'input', 'placeholder': "Entrez le nom de l'auteur"}}) }}
</div>
</div></div>
<div class="grid2">
<div class="form_elem_wrraper">
<div class="row_form">
{{ form_label(form.price, "Price ") }}
{# Génération de l'input. #}
{{ form_widget(form.price,{ 'attr': {'class' :'input', 'placeholder': "Entrez le nom de l'auteur"}}) }}
</div>
<div class="row_form">
{{ form_label(form.Photo, "Photo") }}
{# Génération de l'input. #}
{{ form_widget(form.Photo) }}
</div>
</div>
</div>
</div>
</div>
{# Pour le bouton, pas de label ni d'erreur, on affiche juste le widget #}
{{ form_widget(form.enregistrer,{'attr': {'class': 'btn_bleu btn_submit_form'}}) }}
{# Fermeture de la balise <form> du formulaire HTML #}
{{ form_end(form) }}
</form>
I need your help, Thank you
</div>
Symfony's file form types are not propagated with data from backend. If you would like to show existing file, you have to add it manually in your Twig file.
As you didn't attached your entity code, please note that my example should be used with proper methods from your MyProject\ProductBundle\Entity\Photo
class.
In my example you have to pass to the view Photo entity as photo
, but only if there was already uploaded file before.
<div class="row_form">
{% if photo is defined %}
Current photo: <img src="{{ photo.getWebPath() }}" />
{% endif %}
{{ form_label(form.Photo, "Photo") }}
{# Génération de l'input. #}
{{ form_widget(form.Photo) }}
</div>