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?
$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