Search code examples
symfonyvichuploaderbundle

Problem with Vichuploader and Symfony Validion


I have this form, a report with oneToMany document attached

class ReportType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('documentDatas', CollectionType::class, array(
                'entry_type' => DocumentType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'label' => false
            ))
            ->add('comment', TextType::class, array(
            'label' => 'vat',
            'required' => false,
             ))
            ->add('save', SubmitType::class);
    }
}

and this is the document Type

class DocumentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('examDocument', VichImageType::class, array(
                'label' => 'examDocument',
                'data_class' => null,
                'attr' => array('class' => 'upload-image'),
            ))
            ->add('note', TextType::class, array(
                'label' => 'notes',
                'required' => false,
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Model\DocumentData',
        ));
    }
}

Where I use the VichImageType to upload. When the form validation is ok, all this go right, the file is uploaded and the document entities add in my DB. But when some validation violated (on the document or on the comment) I receive this strange error:

The class "AppBundle\Model\DocumentData" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add @Vich\Uploadable on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.

I receive this when my action try response with the view, when form is not valid:

public function commentAction(Request $request) {
        $reportData = new ReportData();

    $form = $this->createForm(ReportType::class, $reportData);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()){

        //some logic
    }

    return $this->render('report/form.html.twig', [
        'form' => $form->createView(),  //symfony evidence this row in the exception
    ]);
}

Solution

  • I resolve adding the Vich Uploadable annotation mapping also in my data Model, like this:

    /**  * @Vich\Uploadable  */ class DocumentData {
        /**
         * @Assert\NotNull()
         * @Assert\File(
         *     maxSize = "5M",
         *     mimeTypes = {"image/*", "application/pdf"}
         * )
         * @Vich\UploadableField(mapping="document_file", fileNameProperty="examDocument")
         */
        public $examDocument;
    
        /**
         * @Assert\Length(
         *      min = 2,
         *      max = 30,
         * )
         */
        public $note;
    
        public function getExamDocument()
        {
            return $this->examDocument;
        }
    
        public function setExamDocument($examDocument)
        {
            $this->examDocument = $examDocument;
        }
    
        public static function create(Document $document)
        {
            $edm = new static();
            $edm->examDocument = $document->getExamDocument();
            $edm->note = $document->getNote();
    
            return $edm;
        } }
    

    Thanks to Garak for the help!