在 Twig 中显示 flash 消息的正确方法
Correct way of displaying flash messages in Twig
我有一个注册表,提交后,显示快速消息并重定向到特定 pages.My 如果没有错误,快速成功消息工作正常 found.However,错误消息不会显示,当有错误示例空白字段,而是显示默认的 Symfony2 异常。
(DBALexception, PDOexception,SQLexceoption,etc)
[2/2] DBALException: An exception occurred while executing '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 中做到这一点,它扩展了基础 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 page.Otherwise 它会创建一个成功的 flash message.I想以相同的方式进行 html5 验证,其中每个错误都以表单
呈现
你不能那样做。异常'break'程序的流程,遇到异常不能简单的继续执行代码。
但是,您可以 手动 捕获代码中的异常,然后像这样显示出错的地方:
if ($form->isValid()) {
try{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
} catch(\Exception $e){
$this->addFlash('notice', sprintf('A %s was thrown when trying to persist
the entities with message = %s', get_class($e), $e->getMessage());
}
// Rest of your code ...
// Take note that when you encounter exception the flow of the program
// is 'broken' and you must carefully examine what happened to avoid
// unintended consequences and stuff like null valued variables
创建 YML 格式的约束条件
例如
Project\Bundle\YourBundle\Entity\Vo:
properties:
firstname:
- NotBlank: ~
我有一个注册表,提交后,显示快速消息并重定向到特定 pages.My 如果没有错误,快速成功消息工作正常 found.However,错误消息不会显示,当有错误示例空白字段,而是显示默认的 Symfony2 异常。
(DBALexception, PDOexception,SQLexceoption,etc) [2/2] DBALException: An exception occurred while executing '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 中做到这一点,它扩展了基础 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 page.Otherwise 它会创建一个成功的 flash message.I想以相同的方式进行 html5 验证,其中每个错误都以表单
呈现你不能那样做。异常'break'程序的流程,遇到异常不能简单的继续执行代码。
但是,您可以 手动 捕获代码中的异常,然后像这样显示出错的地方:
if ($form->isValid()) {
try{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
} catch(\Exception $e){
$this->addFlash('notice', sprintf('A %s was thrown when trying to persist
the entities with message = %s', get_class($e), $e->getMessage());
}
// Rest of your code ...
// Take note that when you encounter exception the flow of the program
// is 'broken' and you must carefully examine what happened to avoid
// unintended consequences and stuff like null valued variables
创建 YML 格式的约束条件
例如
Project\Bundle\YourBundle\Entity\Vo:
properties:
firstname:
- NotBlank: ~