When I create a new product, I load the required image and everything is fine.
But when I open the page with the form for editing, again ask to upload the file, because the field with the picture is mandatory, how do I insert an existing file into the form or how to get around this point?
Product:
class Product {
/**
* @var string
* @Assert\NotBlank
*/
private $image;
// ...
}
ProductType:
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('image', FileType::class, [
'data_class' => null
])
;
}
// ...
}
As @JimPanse told you, you should read the documentation https://symfony.com/doc/current/controller/upload_file.html which explain you clearly how to manager file upload. Display the file is not the default behavior
After that, you should add a line into your twig to display your image
form.vars.value.image
will return the value of your field image
Add this to your twig
{% if form.vars.value.image is defined and form.vars.value.image is not null %}
<img alt="product" src="{{ form.vars.value.image }}"/>
{% endif %}
Or another way is to override the FileType
template