我用这段代码创建了我的表单:
public function creerConferenceAction($id = null) {
$message = '';
if (isset($id)) {
$conference = $this->getDoctrine()
->getRepository('gestionConferenceApplicationBundle:Conference')
->find($id);
if (!$conference) {
throw $this->createNotFoundException('No conference found for id ' . $id);
}
}else{
$conference = new Conference();
}
$form = $this->createFormBuilder($conference)
->add('titre', 'text')
->add('ville', 'text')
->add('lieu', 'text')
->add('date_debut', 'date', array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
))
->add('date_fin', 'date')
->add('nbMin', 'integer')
->add('nbMax', 'integer')
->add('dateLimiteInscription', 'date')
->getForm();
$request = $this->getRequest();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($conference);
$em->flush();
return $this->redirect($this->generateUrl('_acceuil'));
}
}
return $this->render('gestionConferenceApplicationBundle:acceuil:creerConference.html.twig', array(
'form' => $form->createView(),
));
}
我想添加这个约束:用户不能在这些字段中输入小于 5 的值:
->add('nbMin', 'integer')
->add('nbMax', 'integer')
我对此进行了测试(我从参考书中获取):
# src/gestionConference/ApplicationBundle/Resources/config/validation.yml
gestionConference\ApplicationBundle\Entity\Conference:
properties:
age:
- Min: { limit: 18, message: You must be 18 or older to enter. }
但它什么也不做
我怎样才能做到这一点
先感谢您
-------------------编辑-------编辑---------- --------------------
我再次阅读了参考资料,我意识到 min 选项在最新版本的 symfony 2.1.3 中被删除了
我将其替换为:
# src/gestionConference/ApplicationBundle/Resources/config/validation.yml
gestionConference\ApplicationBundle\Entity\Conference:
properties:
nbmin:
- Range:
min: 120
minMessage: You must be at least 120cm tall to enter
但没有变化
这是树枝页面:
{% extends "gestionConferenceApplicationBundle::layout.html.twig" %}
{% block content %}
<div id="welcome">
<div class="content">
<form action="" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
<table>
<tr>
<td>{{ form_label(form.titre, 'Titre : ') }}</td>
<td>{{ form_widget(form.titre) }}</td>
<td>{{ form_errors(form.titre) }}</td>
</tr>
<tr>
<td>{{ form_label(form.ville, 'Ville : ') }}</td>
<td>{{ form_widget(form.ville) }}</td>
<td>{{ form_errors(form.ville) }}</td>
</tr>
<tr>
<td>{{ form_label(form.lieu, 'Lieu : ') }}</td>
<td>{{ form_widget(form.lieu) }}</td>
<td>{{ form_errors(form.lieu) }}</td>
</tr>
<tr>
<td>{{ form_label(form.date_debut, 'Date de début : ') }}</td>
<td>{{ form_widget(form.date_debut) }}</td>
<td>{{ form_errors(form.date_debut) }}</td>
</tr>
<tr>
<td>{{ form_label(form.date_fin, 'Date de fin : ') }}</td>
<td>{{ form_widget(form.date_fin) }}</td>
<td>{{ form_errors(form.date_fin) }}</td>
</tr>
<tr>
<td>{{ form_label(form.nbMin, 'Nombre minimal de participants : ') }}</td>
<td>{{ form_widget(form.nbMin) }}</td>
<td>{{ form_errors(form.nbMin) }}</td>
</tr>
<tr>
<td>{{ form_label(form.nbMax, 'Nombre maximal de participants : ') }}</td>
<td>{{ form_widget(form.nbMax) }}</td>
<td>{{ form_errors(form.nbMax) }}</td>
</tr>
<tr>
<td>{{ form_label(form.dateLimiteInscription, 'Date limite d inscription : ') }}</td>
<td>{{ form_widget(form.dateLimiteInscription) }}</td>
<td>{{ form_errors(form.dateLimiteInscription) }}</td>
</tr>
{{ form_rest(form) }}
<tr>
<td align="center" colspan=3 >
<input type="submit" style="width: 80px;height: 30px;margin-right: 25px;" value="valider" />
<input type="reset" style="width: 80px;height: 30px;" value="initialiser" />
</td>
</tr>
</table>
</form>
</div>
</div>
{% endblock %}