表单未在Symfony 2.8中显示错误

时间:2018-07-23 05:12:42

标签: php symfony symfony-forms

表单正在验证,并且错误也显示在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)

1 个答案:

答案 0 :(得分:0)

只需删除此行

func setupDatePicker(){

   var currDateComponents = getDateComponents(fromDate: Date())
   var maxDateComponents = currDateComponents

   if let date = currDateComponents?.day{
        maxDateComponents?.day = date + 7
   }

   self.datePicker?.maximumDate = getDate(fromDateComponents: maxDateComponents)

}

func getDateComponents(fromDate date: Date?)-> DateComponents?{
    let calendar = Calendar(identifier: .gregorian)
    var dateComponents: DateComponents? = nil
    if(date != nil){
        dateComponents = calendar.dateComponents([.day, .month, .year], from: date!)
    }

    return dateComponents
 }

 func getDate(fromDateComponents dateComponents:DateComponents?)-> Date?{
    let calendar = Calendar(identifier: .gregorian)

    if let componentsBasedDate = calendar.date(from: dateComponents!) {
        return componentsBasedDate
    }

    return nil
 }

因为此行覆盖了单个字段错误消息的呈现。

在这种情况下,

小枝表单字段名称应根据实体属性名称 小骆驼案

  {{ form_errors(form) }}