【发布时间】:2015-08-17 03:11:35
【问题描述】:
我有一个注册表单,提交后,显示flash消息并重定向到特定页面。如果没有发现错误,我的flash成功消息工作正常。但是,错误消息不会显示,当有错误示例空白字段时,但是会显示默认的 Symfony2 异常。
(DBAL 异常、PDO 异常、SQL 异常选项等) [2/2] DBALException: 执行'INSERT INTO voters (blah and blah......)时发生异常
这是在开发阶段,我想测试错误消息,我想再次显示表单并且错误闪烁而不是 Symfony2 500 错误页面;
如何在特定页面中暂时禁用 Symfony2 异常,而在开发过程中显示错误提示信息?
我在控制器中有这个
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$this->addFlash('notice', 'Welcome to the growing lists of Supporters, dont forget to share and invite this to your friends and relatives, have a magical day!');
//return $this->redirect($this->generateUrl('vo_show', array('id' => $entity->getId())));
return $this->redirect($this->generateUrl('vo'));
}
else {
$this->addFlash('error', 'Welcome to the Death Star, have a magical day!');
return $this->render('Bundle:Vo:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
在树枝上
{% if app.session.flashBag.has('notice') %}
<div class="alert alert-success fade in" role="alert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{% for msg in app.session.flashBag.get('notice') %}
{{ msg }}
{% endfor %}
</div>
{% endif %}
{% if app.session.flashBag.has('error') %}
<div class="alert alert-error fade in" role="alert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{% for msg in app.session.flashBag.get('error') %}
{{ msg }}
{% endfor %}
</div>
{% endif %}
//registerform.twig.html
{{ form_start(form, {attr: {novalidate: 'novalidate'}} ) }}
{{ form_errors(form) }}
{{ form_row(form.comments,{'attr': {'placeholder': 'Why You Want Death'}}) }}
{{ form_end(form) }}
因此,我不想显示 Symfony2 异常,而是希望重定向回表单并在表单提交失败时显示单个表单错误
在 Symfony 1.4 中,我可以在 form.class 中轻松完成,它像这样扩展基类
$this->mergePostValidator(new sfValidatorDoctrineUnique(array(
'model' => 'Voters',
'column' => array('firstname', 'middlename', 'lastname', 'city_id', 'birthday', 'profession_id')),array('invalid' => '<div class="alert alert-warning">You are already registered.No need to proceed buddy</div>')
));
如果填写表单时出现错误,它会在网页中创建一个flash错误消息并重新显示表单而不是重定向到501页面。否则它将创建一个成功的flash消息。我也想这样做html5验证方式,每个错误都以表单呈现
【问题讨论】: