E
E
Eugene Wolf2016-11-13 19:17:44
symfony
Eugene Wolf, 2016-11-13 19:17:44

FileType, why is the data_class property needed?

Good day dear!
I'm learning Symfony3, I went through half a dozen options for creating / processing a field for uploading several files at once.
Among other things, when creating/adding a field of the FileType type , this field itself has the data_class attribute . Why the form itself has such an attribute - I understand, but why FileType has this attribute is not entirely clear to me.
VendorFile- I created this class myself, for testing
Example code:

class VendorType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('www')
            ->add('file1', FileType::class, [
                'multiple' => 'multiple',
                'data_class' => VendorFile::class, //вот эта строчка кода у меня вызывает непонятки
            ])
            ->add('Готово', SubmitType::class);
    }

In what case data_class, for what purposes can it be used? If not difficult, please give a simple example of how and when it can be used? If possible, I would also like to hear a little theory on this topic.
I also don't understand why the class constructor is not called VendorFile? I imagined that the properties of this class should be initialized with the properties of the UploadFile object, but apparently this is not the case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-11-13
@prototype_denis

->add('file1', FileType::class, [
    'multiple' => 'multiple', <-- Это булево свойство и оно не будет работать в данном случае
    'data_class' => VendorFile::class, //вот эта строчка кода у меня вызывает непонятки
])

data_class is needed to map data from $_FILES to an object.
symfony.com/doc/current/reference/forms/types/file...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question