如何获取所有表单错误symfony2

时间:2013-07-18 11:19:23

标签: forms symfony constraints

约束:

/**
 * @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;

Controller(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)字段 - 我没有错误。

1 个答案:

答案 0 :(得分:3)

请在模板中使用{{ form_row(form.name) }}代替{{ form_widget(form.name)来解决问题....

form_widget仅呈现字段的html,而form_row呈现form_labelform_widgetform_errors的组合。

查看文档here

如果您的表单冒充全局错误,请尝试在表单的默认选项中将error_bubbling设置为false(默认为true),如下所示:

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