-1

Is there any way to upload several images with vich? I have searched for related information but I have not found anything useful

I have tried to make the img field a collection in the constructor but I don't know the way to upload more files

I have not found anything in the official documentation of Vich either

This is my Type:

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nombre')
            ->add('img',VichFileType::class,[
                'multiple' => true,
                'label' => 'Imagen',
                'delete_label' => 'Borrar Imagen (Selecciona esta opcion y pulsa en guardar)',
                'download_label' => 'Descargar imagen',
                'constraints' => [
                    new File([
                        'maxSize' => '5M',
                        'mimeTypes' => ['image/png', 'image/jpeg'],
                        'mimeTypesMessage' => 'El archivo tiene que ser una imagen png o jpg'
                    ])
                ]
            ])
            ->add('precio')
            
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Article::class,
        ]);
    }
}

and this is my Entity

#[Vich\Uploadable]
#[ORM\Entity(repositoryClass: ProductosRepository::class)]
class Article
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 40)]
    private ?string $nombre = null;

    #[ORM\Column]
    private ?float $precio = null;

    #[Vich\UploadableField(mapping: 'article', fileNameProperty: 'imageName')]
    private ?File $img = null;

    #[ORM\Column(nullable: true)]
    private ?string $imageName = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $updatedAt = null;


    public function __construct()
    {

    }

    public function setImg(?File $img = null): void
    {
        $this->img = $img;

        if (null !== $img) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTimeImmutable();
        }
    }

    public function getUpdatedAt(): ?string
    {
        return $this->updatedAt;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNombre(): ?string
    {
        return $this->nombre;
    }

    public function setNombre(string $nombre): static
    {
        $this->nombre = $nombre;

        return $this;
    }

    public function getPrecio(): ?float
    {
        return $this->precio;
    }

    public function setPrecio(float $precio): static
    {
        $this->precio = $precio;

        return $this;
    }

    public function getImg(): ?File
    {
        return $this->img;
    }

    public function setImageName(?string $imageName): void
    {
        $this->imageName = $imageName;
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

}

0 Answers0