我正在尝试使用{{form_widget(form)}}在symfony2中自定义我的表单 所以我的buildForm是这样的:
$builder
->add('Status', 'choice', array(
'choices' => array('Required' => 'Required', 'Free' => 'Free', 'OnArrival' => 'OnArrival', 'Refused' => 'Refused'),
'required' => true,
'label' => 'Visa required?'
))
->add('Length', null, array('required' => false, 'label' => 'Max Stay'))
->add('Description', 'textarea',array('required' => false, 'label' => 'Additional information'));
我的twig html:
{% block choice_widget %}
<div class="row">
<div class="col-md-8 ">
<select class="selectpicker " data-style="btn-primary">
{% spaceless %}
{% for choice in choices %}
<option value="{{ choice.value }}">{{ choice.value }}</option>
{% endfor %}
{% endspaceless %}
</select>
</div>
</div>
{% endblock %}
{% block text_widget %}
<div class="row">
<div class="col-md-8 ">
<input id="id_max" type="text" placeholder=" x Month(s)" class="form-control">
</div>
</div>
{% endblock %}
{% block textarea_widget %}
<textarea id="id_desc" type="" placeholder="" class="form-control"></textarea>
{% endblock %}
它很好地展示了应用的新样式,但是当我执行我的表单时,它返回一个异常,并说值为null,即:Status=null , length=null, description=null
PS:当我检索自定义HTML时,它运作良好
编辑:添加控制器部分
/**
* Creates a new TravelInfoCorrection entity.
*
* @Route("/correct/create", name="correct_create")
* @Method("POST")
* @Template("TechyVisaBundle:edit:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new TravelInfoCorrection();
$form = $this->createForm(new TravelInfoCorrectionType(), $entity);
$form->bind($request);
$entity->setCreatedAt(new \DateTime('now'));
$entity->setProcessedFlag('Pending');
if (! $form->isValid()) throw new \Exception('Form not valid');
$em = $this->getDoctrine()->getManager();
$orientity = $em->getRepository('TechyVisaBundle:TravelInfo')
->findOneBy(array( 'TravFrom' => $entity->getTravFrom(), 'TravTo' => $entity->getTravTo() ));
$isdiff = false;
$isdiff = $isdiff || ($entity->getStatus() != $orientity->getStatus());
$isdiff = $isdiff || ($entity->getLength() != $orientity->getLength());
$isdiff = $isdiff || ($entity->getDescription() != $orientity->getDescription());
if ($isdiff){
$em->persist($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('thanks'));
}
和来电者html:
{% form_theme form 'TechyVisaBundle:Form:fields.html.twig' %}
{% block content %}
<br/>
<div id="sign_up1">
<div class="container">
<h2>Edit information {{ from.getName }} to {{ to.getName }} travelling information</h2>
<b style="color: red;">Please verify your informations <a target="_blank" rel="nofollow" href="http://www.gulfair.com/English/info/prepare/Pages/VisaInformation.aspx">here</a> before posting it</b>
<br/>
<form action="{{ path('correct_create') }}" method="post" {{ form_enctype(form) }} class="form-inline">
{{ form_widget(form) }}
<button type="submit">Submit correction</button>
</form>
</div>
</div>
{% endblock %}