[英]Silex Form Choice field
我正在使用Symfony表單和Twig創建過濾器,但是發生了一些奇怪的事情,我不知道為什么。 我有4個選擇字段,前2個選擇字段返回正確的值,但后2個選擇字段返回與第二個選擇字段相同的值。
這是控制器的代碼
// 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));
這是樹枝模板的代碼
<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>
當然使用的4個數組不同,有人可以幫我嗎?
我找到了解決方案,即使我的數據庫在utf8mb5_general_ci中並且我在模板中使用UTF-8,某些html字符也未正確編碼,這些字符顯示為``。 看起來Silex Forms無法使用未編碼的字符來處理選擇字段。 從來沒有想過這兩個問題之間存在聯系,但這只是使用utf8_encode()方法填充選擇字段中使用的數組的問題。 在我的樹枝模板中
// 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);
}
在我的Twig模板中
{{ data|convert_encoding('UTF-8', 'ASCII') }}
像這樣: http : //twig.sensiolabs.org/doc/filters/convert_encoding.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.