dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

The option "required" does not exist ? #1430

Closed valentin-harrang closed 7 months ago

valentin-harrang commented 7 months ago
Q A
Bundle version 1.12.0
Symfony version 5.0.11
PHP version 7.3.33

Support Question

I have a Symfony project in version 5.0.11 on which I use the Vich Uploader bundle in version 1.12 (a fairly old version 😅). There's a problem that I haven't been able to fix, I need your help please.

When I click on a button that should display a form I get a 500 error with this message in prod.log:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException: "An error has occurred resolving the options of the form "Vich\UploaderBundle\Form\Type\VichImageType": The option "required" does not exist. Defined options are: "action", "allow_delete", "allow_extra_fields", "allow_file_upload", "asset_helper", "attr", "attr_translation_parameters", "auto_initialize", "block_name", "block_prefix", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "delete_label", "disabled", "download_label", "download_link", "download_uri", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "help_attr", "help_html", "help_translation_parameters", "image_uri", "imagine_pattern", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "label_translation_parameters", "mapped", "method", "post_max_size_message", "property_path", "required", "row_attr", "storage_resolve_method", "translation_domain", "trim", "upload_max_size_message", "validation_groups"." at /home/jcateppu/public_html/vendor/symfony/form/ResolvedFormType.php line 99 {"exception":"[object] (Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException(code: 0): An error has occurred resolving the options of the form \"Vich\UploaderBundle\Form\Type\VichImageType\": The option \"required\" does not exist. Defined options are: \"action\", \"allow_delete\", \"allow_extra_fields\", \"allow_file_upload\", \"asset_helper\", \"attr\", \"attr_translation_parameters\", \"auto_initialize\", \"block_name\", \"block_prefix\", \"by_reference\", \"compound\", \"constraints\", \"csrf_field_name\", \"csrf_message\", \"csrf_protection\", \"csrf_token_id\", \"csrf_token_manager\", \"data\", \"data_class\", \"delete_label\", \"disabled\", \"download_label\", \"download_link\", \"download_uri\", \"empty_data\", \"error_bubbling\", \"error_mapping\", \"extra_fields_message\", \"help\", \"help_attr\", \"help_html\", \"help_translation_parameters\", \"image_uri\", \"imagine_pattern\", \"inherit_data\", \"invalid_message\", \"invalid_message_parameters\", \"label\", \"label_attr\", \"label_format\", \"label_translation_parameters\", \"mapped\", \"method\", \"post_max_size_message\", \"property_path\", \"required\", \"row_attr\", \"storage_resolve_method\", \"translation_domain\", \"trim\", \"upload_max_size_message\", \"validation_groups\". at /home/jcateppu/public_html/vendor/symfony/form/ResolvedFormType.php:99)\n[previous exception] [object] (Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException(code: 0): The option \"required\" does not exist. Defined options are: \"action\", \"allow_delete\", \"allow_extra_fields\", \"allow_file_upload\", \"asset_helper\", \"attr\", \"attr_translation_parameters\", \"auto_initialize\", \"block_name\", \"block_prefix\", \"by_reference\", \"compound\", \"constraints\", \"csrf_field_name\", \"csrf_message\", \"csrf_protection\", \"csrf_token_id\", \"csrf_token_manager\", \"data\", \"data_class\", \"delete_label\", \"disabled\", \"download_label\", \"download_link\", \"download_uri\", \"empty_data\", \"error_bubbling\", \"error_mapping\", \"extra_fields_message\", \"help\", \"help_attr\", \"help_html\", \"help_translation_parameters\", \"image_uri\", \"imagine_pattern\", \"inherit_data\", \"invalid_message\", \"invalid_message_parameters\", \"label\", \"label_attr\", \"label_format\", \"label_translation_parameters\", \"mapped\", \"method\", \"post_max_size_message\", \"property_path\", \"required\", \"row_attr\", \"storage_resolve_method\", \"translation_domain\", \"trim\", \"upload_max_size_message\", \"validation_groups\". at /home/jcateppu/public_html/vendor/symfony/options-resolver/OptionsResolver.php:799)"} []

If I switch to "dev" mode in the .env.local file I don't get any more errors, I can access the page. If I delete the cache and refresh the web page it works but if I refresh it a 2nd time I get the 500 error again. It's a rather strange problem and I haven't found an answer in the issues or anything else.

My form:

<?php

declare(strict_types = 1);

namespace App\Form\Admin;

use App\Entity\RealisationSousTitreContenu;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\TextEditorType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;

class RealisationSousTitreContenuType extends AbstractType
{
    /** @SuppressWarnings(PHPMD.UnusedFormalParameter) */
    /** @noinspection PhpMissingParentCallCommonInspection */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('sousTitre', TextType::class, [
                'label' => 'Sous-titre',
                'attr'  => [
                    'maxlength' => '140',
                ],
            ])
            ->add('contenu', TextEditorType::class, [
                'label' => 'Contenu',
                'attr'  => [
                    'maxlength' => '8000',
                    'rows'      => 10,
                ],
            ])
            ->add('imageFile', VichImageType::class, [
                'label'    => 'Image',
                'required' => false,
                'attr'     => [
                    'help' => 'Votre image doit mesurer 500 pixels de longueur x 400 pixels de hauteur minimum et être de type jpeg, png ou gif. Il est recommandé d\'utiliser TinyPNG afin d\'optimiser votre image et gagner en performances.',
                ],
            ]);
    }

    /** @noinspection PhpMissingParentCallCommonInspection */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => RealisationSousTitreContenu::class,
        ]);
    }
}

I tried to comment 'required' => false, but same problem.

Thanks in advance

garak commented 7 months ago

The option exists, it's even listed in your error message. It's not even defined in this bundle, so there's nothing to do here. Moreover, according to the policy declared in the documentation, there's no support for Symfony versions in EOL. I can only suggest to upgrade to Symfony 5.4

valentin-harrang commented 7 months ago

@garak I upgraded to Symfony 5.4.35 and Vich Uploader 1.23.1 but same error.

What's strange is that the error isn't present in the dev environment. The error is only present in the prod environment. If I clear the cache and refresh the page once it works but if I refresh a second time I get the error.

valentin-harrang commented 7 months ago

@garak Upgrade Easy Admin 2 to Easy Admin 3 has solved my problem.