I submit the form and process it in the Sylius ResourceController
, which submits the form and validates it.
This is the in situ form:
<tr> <form method="post" action="{{ path('backend_course_row_update', { 'courseeId' : course.id, 'id' : row.id }) }}" novalidate> {{ form_widget(form.channel) }} {{ form_widget(form.name) }} {% for size in form.sizes %} {{ form_row(size) }} {% endfor %} {{ form_row(form._token) }} <td align="right" style="width: 140px;"> <button class="btn btn-primary" type="submit"> <i class="glyphicon glyphicon-save"></i>Save </button> </td> </form> </tr>
The "form" here is CourseGuideRowType
, which is as follows:
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('channel', 'channel_choice', array( 'required' => false )) ->add('name', 'text') ->add('sizes', 'course_guide_row_sizes', array('numColumns' => $options['numColumns'])) ; }
CourseGuideRowSizesType
as follows:
public function buildForm(FormBuilderInterface $builder, array $options) { for ($i = 0; $i < $options['numColumns']; $i++) { $builder->add($i, 'text', array('required' => 'false')); } $builder->addEventListener( FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) { $form = $event->getForm(); for ($i = 0; $i < $options['numColumns']; $i++) { if (empty($form->get($i)->getData())) { $form->remove($i, 'text'); } } } ); }
However, when I submit the form and upload errors like this:
$form->submit($request, !$request->isMethod('PATCH'))->getErrors()
I get:
"This form should not contain extra fields." #messageParameters: array:1 [▼ "{{ extra_fields }}" => "0", "1", "2", "3", "4", "5" ] -extraData: array:6 [▼ 0 => "36" 1 => "38" 2 => "40" 3 => "42" 4 => "44" 5 => "46" ]
"Additional data" is the "size" field.
Am I doing something obviously wrong here?
EDIT June 2017: now you can use 'allow_extra_fields' to suppress this error. http://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields