Skip to content
Advertisement

Error with Symfony The option “constraints” does not exist

Symfony question, I’m just starting to learn it. The user uploads a file with data that I want to submit to the form for validation in the form of an array (key-value). I call the method to build the form, pass an array there and try to check, for example, that the length of the title (‘title’) is no more than 255 characters. The error “The option “constraints” does not exist” is thrown.

ImportService.php

<?php

namespace AppService;

use AppEntityImport;  
use AppServiceServiceInterface;  
use BoxSpoutReaderCommonCreatorReaderEntityFactory;  
use SymfonyComponentFormForms;  
use DoctrinePersistenceManagerRegistry; 
use SymfonyComponentFormForm;

class ImportService implements ServiceInterface {
    public function __construct(protected ManagerRegistry $doctrine)
    {
    }
 
    public function parse(Import $import, array $scheme, array $formOptions = [])
    {
        $rowArray = [];

        $reader = ReaderEntityFactory::createXLSXReader();
        $reader->open($import->getPath());
        foreach ($reader->getSheetIterator() as $index => $sheet) {
            foreach ($sheet->getRowIterator() as $rowIndex => $row) {
                if ($rowIndex == 1) {
                    continue;
                } else {
                    $cells = $row->getCells();
                    foreach ($cells as $cell) {
                        $rowArray[] = $cell->getValue();
                    };

                    $row = array_combine($scheme, $rowArray);

                    $importRow = new ImportRow($import, $sheet, $rowIndex, $row);

                    $form = $this->buildForm($import->getFormType());
                    $form->submit($row);

                    if ($form->isValid()) {
                        $import->setSuccess('true');
                        $this->saveImport($import);

                        return $row;
                    }
                }
            };
        }
    }

    protected function buildForm(string $formType, array $formOptions = []): Form
    {
        $formFactory = Forms::createFormFactory();

        return $formFactory
            ->create(
                $formType,
                null,

            );
    }

NewsImportType.php

use AppEntityNews; use SymfonyComponentFormAbstractType;  
use SymfonyComponentFormFormBuilderInterface;  
use SymfonyComponentOptionsResolverOptionsResolver;  
use SymfonyComponentFormExtensionCoreTypeTextType; 
use SymfonyComponentFormExtensionCoreTypeTextareaType; 
use SymfonyComponentValidatorConstraintsCollection; 
use SymfonyComponentValidatorConstraintsEmail; 
use SymfonyComponentValidatorConstraintsLength; 
use SymfonyComponentValidatorConstraintsUrl;

class NewsImportType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, [
                'constraints' =>
                [new Length(['max' => 256])],
            ])
            ->add('text', TextareaType::class, [
                'constraints' =>
                [new Length(['max' => 1000])],
            ])
            ->add('image', TextType::class, [
                'constraints' =>
                [
                    new Length(['max' => 256]),
                    new Url()
                ],
            ]);
    }

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

Error text

An error has occurred resolving the options of the form “SymfonyComponentFormExtensionCoreTypeTextType”: The option “constraints” does not exist. Defined options are: “action”, “allow_file_upload”, “attr”, “attr_translation_parameters”, “auto_initialize”, “block_name”, “block_prefix”, “by_reference”, “compound”, “data”, “data_class”, “disabled”, “empty_data”, “error_bubbling”, “form_attr”, “getter”, “help”, “help_attr”, “help_html”, “help_translation_parameters”, “inherit_data”, “invalid_message”, “invalid_message_parameters”, “is_empty_callback”, “label”, “label_attr”, “label_format”, “label_html”, “label_translation_parameters”, “mapped”, “method”, “post_max_size_message”, “priority”, “property_path”, “required”, “row_attr”, “setter”, “translation_domain”, “trim”, “upload_max_size_message”.

ALTERNATIVE SOLUTION

Add ‘FormFactoryInterface $formFactory’ to the constructor. And in the method itself, fix to ‘$this->’

return $this->formFactory
            ->create(
                $formType,
                null,
                array_merge($formOptions, $baseOptions)

Advertisement

Answer

The constraints option is part of ValidatorExtension and it is not part of core form extensions. You can use it like following

$validator = Validation::createValidator();
$formFactory = Forms::createFormFactoryBuilder()
        ->addExtension(new ValidatorExtension($validator))
        ->getFormFactory();

also add this

use SymfonyComponentFormExtensionValidatorValidatorExtension;

refer link for details

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement