I'm using Symfony forms and Twig to create a filter but something weird is going on and I can't figure out why. I have 4 choice fields, the first 2 choice fields are returning the correct values but the last 2 choice fields are returning the same values as the second choice field.
this is the code for the Controller
// Create Form
$filterForm = $app['form.factory']->createNamed('filterForm')
// search
->add('title', 'text')
// date
->add('dates', 'choice', array(
'choices' => $DatesForCombo,
'placeholder' => 'Kies...',
'required' => false))
// category
->add('categories', 'choice', array(
'choices' => $CategoriesForCombo,
'placeholder' => 'Kies...',
'required' => false))
// location
->add('locations', 'choice', array(
'choices' => $LocationsForCombo,
'placeholder' => 'Kies...',
'required' => false))
// organiser
->add('organisers', 'choice', array(
'choices' => $OrganisersForCombo,
'placeholder' => 'Kies...',
'required' => false))
// is_gentian
->add('gents',null,array(
'label' => 'Enkel Gents gesproken',
'required' => false))
->add('gentsGesproken', 'checkbox', array(
'value' => 'Y',
'required' => false))
// is_free
->add('gratis',null,array(
'label' => 'Enkel gratis events',
'required' => false))
->add('gratisEvents', 'checkbox', array(
'value' => 'Y',
'required' => false));
This is the code for the Twig Template
<form action="{{ path('events.index') }}" method="post" {{ form_enctype(filterForm) }} novalidate="novalidate" role="form">
<fieldset>
<legend>Filter Events</legend>
<div class="form-group">
{{ form_widget(filterForm.title, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[title]', 'id':'filterform_title' }} ) }}
</div>
<!-- filter dates -->
<div class="form-group">
{{ form_widget(filterForm.dates, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[day]', 'id':'filterform_title' }} ) }}
</div>
<!-- filter categories -->
<div class="form-group">
{{ form_widget(filterForm.categories, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[category]', 'id':'filterform_categories' }} ) }}
</div>
<!-- filter location -->
<div class="form-group">
{{ form_widget(filterForm.locations, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[location]', 'id':'locations' }} ) }}
</div>
<!-- filter organisers -->
<div class="form-group">
{{ form_widget(filterForm.organisers, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[organiser]', 'id':'organisers' }} ) }}
</div>
<div class="form-group">
<!-- filter chckbx Gents gesproken -->
<label>
{{ form_widget(filterForm.gentsGesproken) }}
{{ form_label(filterForm.gents) }}
</label>
<!-- filter chckbx Gratis events -->
<label>
{{ form_widget(filterForm.gratisEvents) }}
{{ form_label(filterForm.gratis) }}
</label>
</div>
<input type="hidden" id="filterform__token" name="filterform[_token]" value="lJ3pQTYX8yEbYpDhmHH6V_ktwtbz_5BdWP0Fss6Z7s0" />
<button type="submit" id="filterform_filter" name="filterform[filter]" class="btn btn-primary pull-right">Filter</button>
</fieldset>
</form>
The 4 arrays used are of course different, can someone please help me?
I found the solution, some of the html characters where not encoded correctly, even though my database was in utf8mb5_general_ci and I use UTF-8 in my templates, the characters where shown as a . Looks like Silex Forms can't handle a choice field with a character that isn't encoded. Never thought that those 2 problems where connected but it was just a matter of using the utf8_encode() method to fill the arrays used in the choice fields. In my twig templates I used
// create an array for the organiser combobox...
$OrganisersForCombo = array();
$numberOfOrganisers = count($AllOrganisers);
for($i = 0 ; $i <= $numberOfOrganisers-1 ; $i++){
$title = $AllOrganisers[$i]['title'];
$OrganisersForCombo[$i+1] = utf8_encode($title);
}
In my Twig templates I used
{{ data|convert_encoding('UTF-8', 'ASCII') }}
Like so: http://twig.sensiolabs.org/doc/filters/convert_encoding.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.