1

约束:

/**
 * @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)字段中输入了不正确的数据 - 我没有错误。

4

1 回答 1

3

请使用{{ form_row(form.name) }}而不是{{ form_widget(form.name)在您的模板中来解决问题....

form_widget仅呈现字段的 html,同时form_row呈现form_label,form_widget和. 的组合form_errors

看看这里的文档。

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

$this->createFormBuilder($entity, array(
        'error_bubbling' => false,
        'data_class'     => 'Vendor\MyBundle\Entity\Name',
        // ... more options 
     )
 )
于 2013-07-18T14:48:54.803 回答