E
E
entermix2018-04-21 22:22:46
symfony
entermix, 2018-04-21 22:22:46

Why is the entity update form not working?

Symfony 4.0
Entity:
https://pastebin.com/V7amJQED
Form:

<?php

namespace App\Form\Dashboard\Book;

use App\Entity\Book;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class BookEditType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('cover', FileType::class)
            ->add('slug', TextType::class)
            ->add('name', TextType::class)
            ->add('description', TextareaType::class)
            ->add('plot', TextareaType::class)
            ->add('published_at', DateType::class, [
                'years' => range(date('Y') - 25, date('Y')),
            ])
            ->add('save', SubmitType::class);
        ;
    }

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

When trying to navigate to the address where the form is placed:
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.

If you try like this:
$resolver->setDefaults([
            'data_class' => null,
        ]);

Another error appears:
Cannot read index "cover" from object of type "App\Entity\Book" because it doesn't implement \ArrayAccess.

I thought that the error occurs precisely because of the cover file field), completely removed it from the form - the same error, but from the next. field, etc.
A similar form for adding an entity: https://pastebin.com/0MptL1H4
Everything is OK here, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-04-21
@entermix

It means to set data_class for the cover field. Have a look here: https://github.com/symfony/symfony/blob/833909bd68...
See, data_class is set to Symfony\Component\HttpFoundation\File\File
Try this:
UPD: And in general, you are doing the download incorrectly. See here https://symfony.com/doc/current/controller/upload_...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question