2013-07-18 16 views
1

約束:如何獲得所有形式的錯誤Symfony2的

/** 
* @ORM\Column(type="string", length=15) 
* @Assert\Regex(pattern = "/[0-9a-z]+/", message = "[regexp] ERROR") 
* @Assert\NotBlank(message = "[notblank] ERROR") 
* @Assert\NotNull(message = "[notnull] ERROR") 
* @Assert\Length 
*   (
*   min = "2", 
*   max = "4", 
*   minMessage = "[minlength] ERROR", 
*   maxMessage = "[maxlength] ERROR" 
*   ) 
*/ 
private $type_name; 

/** 
* @ORM\Column(type="string", length=50) 
* @Assert\Regex(pattern = "/[0-9a-zA-Z\.\:\s]+/", message = "[regexp] ERROR") 
* @Assert\NotBlank(message = "[notblank] ERROR") 
* @Assert\NotNull(message = "[notnull] ERROR") 
* @Assert\Length 
*   (
*   min = "4", 
*   max = "50", 
*   minMessage = "[minlength] ERROR", 
*   maxMessage = "[maxlength] ERROR" 
*   ) 
*/ 
private $description; 

/** 
* @ORM\Column(type="string", length=60) 
* @Assert\Regex(pattern = "/[0-9a-zA-Z\.\/]+/", message = "[regexp] ERROR") 
* @Assert\NotBlank(message = "[notblank] ERROR") 
* @Assert\NotNull(message = "[notnull] ERROR") 
* @Assert\Length 
*   (
*   min = "4", 
*   max = "60", 
*   minMessage = "[minlength] ERROR", 
*   maxMessage = "[maxlength] ERROR" 
*   ) 
*/ 
private $starterPath; 

控制器(typesAction和typesAddAction):

public function typesAction() 
{  
    $em = $this->getDoctrine()->getManager(); 
    $types = $em->getRepository('CsmBundle:Type')->findAll(); 

    $newType = new Type(); 
    $form = $this->createFormBuilder($newType) 
     ->add('typeName', 'text') 
     ->add('description', 'text') 
     ->add('starterPath', 'text') 
     ->getForm(); 

    return $this->render('CsmBundle:Root:types.html.twig', array(
     'types' => $types, 
     'form' => $form->createView() 
    )); 
} 

public function typesAddAction(Request $request) 
{ 
    $newType = new Type(); 

    $form = $this->createFormBuilder($newType) 
     ->add('typeName', 'text') 
     ->add('description', 'text') 
     ->add('starterPath', 'text') 
     ->getForm(); 

    if ($request->getMethod() == 'POST') 
    { 
     $form->bind($request); 

     if ($form->isValid()) 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($newType); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('root_types')); 
     } 
     else 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $types = $em->getRepository('CsmBundle:Type')->findAll(); 

      return $this->render('CsmBundle:Root:types.html.twig', array(
       'types' => $types, 
       'form' => $form->createView() 
      )); 
     } 
    } 
} 

types.html.twig:

... 
<form class="well" action="{{ path('root_types_add') }}" method="post" {{ form_enctype(form) }}> 
<fieldset> 
    <legend>Adding New Type</legend> 
    <table border="0"> 
     <tr> 
      <td width="100" align="left"><strong>Type name:</strong></td><td>{{ form_widget(form.typeName, { 'attr': {'class': 'txt'} }) }}</td> 
     </tr> 
     <tr> 
      <td align="left"><strong>Description:</strong></td><td>{{ form_widget(form.description, { 'attr': {'class': 'txt'} }) }}</td> 
     </tr> 
     <tr> 
      <td align="left"><strong>Starter:</strong></td><td>{{ form_widget(form.starterPath, { 'attr': {'class': 'txt'} }) }}</td> 
     </tr> 
     <tr> 
      <td colspan="2">{{ form_errors(form) }}</td> 
     </tr> 
     <tr> 
      <td colspan="2">{{ form_rest(form) }}</td> 
     </tr> 
     <tr> 
      <td colspan="2" align="right"><button style="" class="btn btn-large btn-success" value="add" name="add">Add!</button></td> 
     </tr> 
    </table> 
</fieldset> 
</form> 
... 

問題有:有隻有第一個字段(typeName)錯誤。 如果我輸入不正確的數據到HTML表單的所有字段,我只有(!)第一個字段(typeName)的一個錯誤。 如果我輸入不正確的數據到第二(描述)和第三(starterPath)字段 - 我沒有錯誤。

+0

的錯誤只是沒有出現在模板或做你的形式認爲輸入是有效的,應該被其他屬性的約束所捕獲? – nifr

+0

控制器代碼和樹枝模板上的描述和starterPath字段沒有錯誤!就好像$ description的約束和starterPath的約束不存在一樣。如果我調用$ form-> getErrors(),我只有第一個字段(typeName)的錯誤,但如果調用$ form-> getErrorsAsString(),我會得到真正的結果並顯示所有錯誤。 – TroyashkA

+0

只是爲了理解它......「在控制器代碼中描述和starterPath字段沒有錯誤」...意味着'$ form-> isValid()'返回'true'? ...而$ form-> getErrorsAsString()包含描述和starterPath的錯誤? – nifr

回答

3

請使用{{ form_row(form.name) }}而不是{{ form_widget(form.name)內的模板,以解決問題....

form_widget只呈現字段的html而form_row呈現的form_labelform_widgetform_errors組合。

看看文檔here

如果形成氣泡了錯誤,全局錯誤,請嘗試將error_bubblingfalse內部窗體的默認選項(默認爲true)是這樣的:

$this->createFormBuilder($entity, array(
     'error_bubbling' => false, 
     'data_class'  => 'Vendor\MyBundle\Entity\Name', 
     // ... more options 
    ) 
)