我在symfony2应用程序中创建了搜索表单。但当我提交表单时,所选值不会在下次呈现时保留
我知道我可以通过selected="selected"实现这一点
但是我怎么能给出GET变量的条件呢,因为trick在中不支持php代码
我的样本代码
<form action="{{ path('dcampaign') }}" method="get" class="doctor">
<select placeholder="Select Chronic Diseases" class="SlectBox" multiple="multiple" name="symptomlist[]" >
{% for symptoms in symptoms_array %}
<option value = {{symptoms.id}} >{{ symptoms.name}}</option>
{% endfor %}
</select>
<input type="submit" value="Create Campaign" class="btn btn-info" id="searchBtn" >
</form>
i tried app.request.query.get(); but doesn't help much...
您应该将其排除在Twig模板之外,并在控制器中执行。就我个人而言,我会使用表单库,所以制作一个带有symptoms
字段的SearchType
。然后在Twig中生成表单。我总是以这种方式实现搜索和筛选字段。
如果你不能或不想使用表单库,那么至少从控制器操作中的请求中获取值,并将其作为变量传递到Twig中。