8
votes

I am using Symfony 2.6. I am trying to create a form without Entity, but get the following error:

The option "constraints" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "compound", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "inherit_data", "intention", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "virtual".

    class MessageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('sender', 'text', [
                'constraints' => [
                    new Constraints\NotBlank(),
                ],
            ])
            ->add('recipient', 'email')
            ->add('message', 'textarea');
    }

    public function getName()
    {
        return 'message';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Constraints\Collection(array(
            'fields' => [
                'sender' => [
                    new Constraints\NotBlank(),
                    new Constraints\Email(),
                ],
                'recipient' => [
                    new Constraints\NotBlank(),
                    new Constraints\Email(),
                ],
                'message' => [
                    new Constraints\NotBlank(),
                ],
            ],

        ));

        $resolver->setDefaults([
            'validation_constraints' => $collectionConstraint,
        ]);
    }
}

Using only setDefaultOptions shows no error, but it does not work, does not validate the fields.

As can be seen, tried anyway. I also tried as is the documentation for using the component form outbox, but get the same error.

http://symfony.com/doc/current/components/form/introduction.html#form-validation

EDIT

I also tried this way and get the same error.

    $form = $formFactory->createBuilder()
    ->add('task', 'text', array(
        'constraints' => new NotBlank(),
    ))
    ->add('dueDate', 'date', array(
        'constraints' => array(
            new NotBlank(),
            new Type('\DateTime'),
        )
    ))
    ->getForm();
3
I feel like what you're trying to do is perfectly valid. Since nothing rings a bell here I would set a breakpoint in buildForm and try following Symfony to see why does it fail...Jovan Perovic
Are you sure this error comes from the constraints option you try to add to the sender text input, and not from somewhere else in your code? I don't see what's wrong either.user3849602
Yes I Understand. I did exactly as it is in Symfony documentation and still the same error.Anderson Scouto da Silva

3 Answers

2
votes

The 'constraints' option is part of the Validator extension form. How I solved the problem:

$ValidatorExtension = new ValidatorExtension($validatorBuilder->getValidator());

$formRegistry = new FormRegistry([$csrfProvider, new CoreExtension(), $ValidatorExtension], Yii::$symfony->container->get('form.resolved_type_factory'));
1
votes

It's quite old question, I just found it, so the problem still appears time to time.

I did like in docs: https://symfony.com/doc/current/components/form.html

$validator = Validation::createValidator();

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

and don't forget about use of course:

use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
0
votes

The 'constraints' option is part of ValidatorExtension which is not part of core form extensions. TypeTestCase it only loads core form extensions. Adding following code fixes the issue.

protected function getExtensions()
{
    return [new ValidatorExtension(Validation::createValidator())];
}

For more information https://symfony.com/doc/current/form/unit_testing.html#adding-custom-extensions