对于我的项目,我使用了Symfony框架。我需要使用select选项来为我的表单生成一个列表。在树枝中选择选项html标记Symfony
下面是代码:
形式:
<form method="post" {{ form_enctype(form)}} action="{{ path('my_path')}}">
{{form_errors(form)}}
<div name="nature">
{{form_label(form.nature,"(*) Nature sample")}}
{{form_errors(form.nature)}}
<select name="nature" id="nature">
<option value ="Other">Other</option>
<option value ="Adn">ADN</option>
</select>
</div>
{{ form_widget(form) }}
<input type="submit" value="Next" class="btn btn-primary" />
</form>
FormType:
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('nature')
->add('origin');
}
控制器:
public function madeDemandAction($id, Request $request)
{
$em = $this-> getDoctrine() -> getManager();
$sample = new Sample();
$repository = $this ->getDoctrine()
->getManager()
->getRepository('BsBundle:Demand')
->find($id);
$demand = $repository;
$form=$this ->createForm(new SampleType, $sample);
if($request ->getMethod() == 'POST')
{
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
dump($request);
$inforequest=$form->getData();
dump($inforequest);
$em = $this->getDoctrine()->getManager();
$em->persist($inforequest);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
}
return $this ->render('bs_connected/human_demand.html.twig'
, array('form'=>$form ->createView()
, 'inforequest'=>$inforequest
));
}
问题是,当我选择一个选项我形式,该字段是n不加载我的数据库。
添加处理表单提交的控制器代码可能是一个好主意。 – tlenss