表单在 Symfony 2.8 中不显示错误
Form is not displaying errors in Symfony 2.8
表单正在验证,错误也在 form_errors(form)
中显示,但是当我尝试 form_errors(form.Title)
时,表单通过重定向到同一页面进行了验证,但没有显示任何错误。
控制器
class CRUDController extends Controller
{
/**
* @Route("/add", name="insert_article_route")
*/
public function insertArticleAction(Request $request)
{
$article = new Article();
// bootstrap class
$class = ["class"=>"form-control"];
$form = $this->createForm(ArticleType::class,$article);
$form->handleRequest($request);
if($request->isMethod('POST'))
{
if($form->isSubmitted() && $form->isValid())
{
// get form data
$article = $form->getData();
// get image file
$file = $article->getImage();
// get original image name
$filename = $file->getClientOriginalName();
// set image name in entity
$article->setImage($filename);
// Image upload
$dir = $this->getParameter('images');
$file->move($dir, $filename);
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->redirectToRoute('table_route');
}
}
return $this->render('BlogBundle:Blog:form.html.twig',['form'=>$form->createView()]);
}
树枝形态
{% extends 'BlogBundle::base.html.twig' %}
{% block nav_item %}
<li class="nav-item active">
<a class="btn btn-secondary" href="{{ url('table_route') }}">View</a>
</li>
{% endblock %}
{% block body %}
<div class="container">
<div class="row mt-5 justify-content-center">
<div class="col-6">
{{ form_start(form, { method:'post',attr:{novalidate:'novalidate'}}) }}
<div class="form-group">
{{ form_errors(form) }}
{{ form_label(form.Title) }}
<div class="text-danger">
{{ form_errors(form.Title) }}
</div>
{{ form_widget(form.Id) }}
{{ form_widget(form.Title) }}
</div>
<div class="form-group">
{{ form_label(form.Description) }}
{{ form_errors(form.Description) }}
{{ form_widget(form.Description) }}
</div>
<div class="form-group">
{{ form_label(form.Content) }}
{{ form_errors(form.Content) }}
{{ form_widget(form.Content) }}
</div>
<div class="form-group">
{{ form_label(form.Category) }}
{{ form_errors(form.Category) }}
{{ form_widget(form.Category) }}
</div>
<div class="form-group">
{{ form_label(form.CreatedAt) }}
{{ form_errors(form.CreatedAt) }}
{{ form_widget(form.CreatedAt) }}
</div>
<div class="form-group">
{{ form_widget(form.Image) }}
{{ form_errors(form.CreatedAt) }}
</div>
<div class="form-group">
{{ form_widget(form.Submit) }}
</div>
</div>
</div>
</div>
{{ form_rest(form) }}
{{ form_end(form, {'render_rest':false}) }}
{% endblock %}
实体文章
<?php
namespace BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="BlogBundle\Repository\ArticleRepository")
*/
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(min="5")
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
* @Assert\NotBlank()
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255)
* @Assert\NotBlank()
*/
private $content;
...
.....
截图
Form errors in form_errors(form)
只需删除这一行
{{ form_errors(form) }}
因为这一行覆盖个别字段错误消息呈现。
twig 表单字段名称应根据实体 属性 名称,在本例中
小驼峰
{{ form_errors(form.xxx) }}
表单正在验证,错误也在 form_errors(form)
中显示,但是当我尝试 form_errors(form.Title)
时,表单通过重定向到同一页面进行了验证,但没有显示任何错误。
控制器
class CRUDController extends Controller
{
/**
* @Route("/add", name="insert_article_route")
*/
public function insertArticleAction(Request $request)
{
$article = new Article();
// bootstrap class
$class = ["class"=>"form-control"];
$form = $this->createForm(ArticleType::class,$article);
$form->handleRequest($request);
if($request->isMethod('POST'))
{
if($form->isSubmitted() && $form->isValid())
{
// get form data
$article = $form->getData();
// get image file
$file = $article->getImage();
// get original image name
$filename = $file->getClientOriginalName();
// set image name in entity
$article->setImage($filename);
// Image upload
$dir = $this->getParameter('images');
$file->move($dir, $filename);
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->redirectToRoute('table_route');
}
}
return $this->render('BlogBundle:Blog:form.html.twig',['form'=>$form->createView()]);
}
树枝形态
{% extends 'BlogBundle::base.html.twig' %}
{% block nav_item %}
<li class="nav-item active">
<a class="btn btn-secondary" href="{{ url('table_route') }}">View</a>
</li>
{% endblock %}
{% block body %}
<div class="container">
<div class="row mt-5 justify-content-center">
<div class="col-6">
{{ form_start(form, { method:'post',attr:{novalidate:'novalidate'}}) }}
<div class="form-group">
{{ form_errors(form) }}
{{ form_label(form.Title) }}
<div class="text-danger">
{{ form_errors(form.Title) }}
</div>
{{ form_widget(form.Id) }}
{{ form_widget(form.Title) }}
</div>
<div class="form-group">
{{ form_label(form.Description) }}
{{ form_errors(form.Description) }}
{{ form_widget(form.Description) }}
</div>
<div class="form-group">
{{ form_label(form.Content) }}
{{ form_errors(form.Content) }}
{{ form_widget(form.Content) }}
</div>
<div class="form-group">
{{ form_label(form.Category) }}
{{ form_errors(form.Category) }}
{{ form_widget(form.Category) }}
</div>
<div class="form-group">
{{ form_label(form.CreatedAt) }}
{{ form_errors(form.CreatedAt) }}
{{ form_widget(form.CreatedAt) }}
</div>
<div class="form-group">
{{ form_widget(form.Image) }}
{{ form_errors(form.CreatedAt) }}
</div>
<div class="form-group">
{{ form_widget(form.Submit) }}
</div>
</div>
</div>
</div>
{{ form_rest(form) }}
{{ form_end(form, {'render_rest':false}) }}
{% endblock %}
实体文章
<?php
namespace BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="BlogBundle\Repository\ArticleRepository")
*/
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(min="5")
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
* @Assert\NotBlank()
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=255)
* @Assert\NotBlank()
*/
private $content;
...
.....
截图
Form errors in form_errors(form)
只需删除这一行
{{ form_errors(form) }}
因为这一行覆盖个别字段错误消息呈现。
twig 表单字段名称应根据实体 属性 名称,在本例中 小驼峰
{{ form_errors(form.xxx) }}