0
votes

I am trying to use Symfony Form Component with Silex framework at the moment. I added some fields in buildForm method of my form type class. Also user can click on a button and add unlimited textarea element using javascript on the frontend. Now on PRE_SUBMIT event, I do the following to add these fields to the form

        $data = $event->getData();
    $form = $event->getForm();
    foreach ($data as $key => $value) {
        if (stristr($key, '_tb_') !== false) {
            $id = str_ireplace('_tb_', '', $key);
            $form->add('_tb_' . $id, 'hidden');
            $form->add('_title_' . $id, 'text', [
                'required'    => false,
                'label'       => 'Title',
                'constraints' => [
                    new Length(['min' => 6]),
                ]
            ]);
            $form->add('_img_url_' . $id, 'text', [
                'required'    => false,
                'label'       => 'Image Url',
                'constraints' => [
                    new Url(),
                ]
            ]);
            $form->add('_img_alt_' . $id, 'text', [
                'required'    => false,
                'label'       => 'Image Alt',
                'constraints' => []
            ]);
            $form->add('_content_' . $id, 'textarea', [
                'required'    => true,
                'attr'        => [
                    'data-role' => '_richeditor'
                ],
                'constraints' => [
                    new Length(['min' => 100]),
                ]
            ]);
        }
    }

I can see these fields are added to the form and populated once the form is submitted for the first time but for some reason all the constraints are ignored only for these new added fields. Is there a way to force Form to honour the constraints for the newly added elements?

1
I've been unable to reproduce this issue from your example -- the constraints used on the dynamic fields are honored in my tests. Can you include the output of $data in your question? Unless you have the mistaken assumption that the 'required'=>true attribute for your content field implies a NotBlank() constraint?user747731
$data would be something like ['_tb_1'=>'','_title_1'=>'','_img_url_1'=>'','_img_alt_1'=>'','_content_1'=>'']. So I am expecting to get an error on _content_1 as it cannot be empty. Am I missing something?Optimus

1 Answers

1
votes

The form component and validation can be tricky. A easy misconception is that the form type option "required" will imply a NotBlank validation constraint. This is not the case, the docs explain that option to be "superficial and independent from validation" only concerned with form element rendering (HTML5 required attr, label, etc).

To make things trickier, that you are specifying a minimum length constraint, one might assume that no (or zero) length would be considered invalid. This is also not the case. The length validator is only concerned with non-null / non-empty values. :-/

So! Modifying the text area field to include NotBlank() should do the trick:

$form->add('_content_' . $id, 'textarea', [
    'required'    => true,
    'attr'        => [
        'data-role' => '_richeditor'
    ],
    'constraints' => [
        new NotBlank(),
        new Length(['min' => 100]),
    ]
]);