Reputation: 39
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 App\Service;
use App\Entity\Import;
use App\Service\ServiceInterface;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Symfony\Component\Form\Forms;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Form\Form;
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 App\Entity\News; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Url;
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 "Symfony\Component\Form\Extension\Core\Type\TextType": 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)
Upvotes: 0
Views: 1981
Reputation: 2314
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 Symfony\Component\Form\Extension\Validator\ValidatorExtension;
Upvotes: 1
Reputation: 149
Acoordind to the doc, you mush have something like that :
->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()
],
]);
You put this :
'constraints' =>
[new Length(['max' => 256])],
])
the doc say this :
'constraints' => new Length(['max' => 256]),
Upvotes: 0
Reputation: 7554
As stated in the documentation:
This option is added in the FormTypeValidatorExtension form extension.
You probably forgot to install the Validator component:
composer require symfony/validator
Upvotes: 2