Symfony2重复类型不验证

时间:2013-07-29 14:45:14

标签: forms validation symfony

我有两个密码字段。额外的是确认。 当两者都为空或密码不匹配时,没有错误消息。

如何启用或调试我认为应该开箱即用的验证(http://symfony.com/doc/current/reference/forms/types/repeated.html#validation)?

class MyType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\\Bundle\\Entity\\User',
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        ...
        $builder->add('password', 'repeated', array(
            'invalid_message' => 'Passwords must match.',
            'type'            => 'password',
            'required'        => true,
            'first_options'   => array('label' => 'Password'),
            'second_options'  => array('label' => 'Repeat Password')
        ));

form.html.twig

...
<form action="" method="post" novalidate>
...
<tr>
    <td>{{ form_label(regForm.password.first) }}</td>
    <td>{{ form_widget(regForm.password.first, { 'attr': {'pattern': '.{6,}','title': 'Minimum 6 characters long'} } ) }}</td>
</tr>
<tr>
    <td>{{ form_label(regForm.password.second) }}</td>
    <td>{{ form_widget(regForm.password.second, { 'attr': {'pattern': '.{6,}', 'title': 'Minimum 6 characters long', 'data-equals': '#register_password_first'} } ) }}</td>
</tr>

validation.yml 中声明的验证器似乎对此问题没有影响。

My\Bundle\Entity\User:
    properties:
        ...
        password:
            # this one does not work because of "repeated" type in buildForm()???
                - NotBlank:
                    message: "Password is required."
                - MinLength:
                    limit: 6
                    message: "Password must have at least {{ limit }} characters."

Controller.php (原始版本)

...
if (!$form->isValid()) {
    $formFields = $form->all();
    foreach ($formFields as $key => $field) {
        if ($field->getErrorsAsString()) {
            $errors = $field->getErrors();
            foreach ($errors as $error) {
                $messages[$key][] = $error->getMessage();
            }
        }
    }
    var_dump($messages);

编辑:现在错误检索看起来像这样(感谢alex.dominte):

public static function getFormErrorMessages(\Symfony\Component\Form\Form $form)
{
    $messages = array();

    foreach ($form->getErrors() as $i => $error) {
        $messages[$i] = $error->getMessage();
    }

    foreach ($form->all() as $child) {
        if (!$child->isValid()) {
            $messages[$child->getName()] = self::getFormErrorMessages($child);
        }
    }

    return $messages;
}

Pedro Cordeiro建议的修改可以删除递归,但也可以使字段名称不可用。

3 个答案:

答案 0 :(得分:3)

您的定义中缺少两件事:

  • 1)必须在您的字段上手动将error_bubbling设置为TRUE,这样他们就可以在 - &gt; getErrors()函数中抛出错误。
  • 2)cascade_validation默认为FALSE,您必须将其添加到您的父表单($builder->add('propertyName', new YourFormNameType(), array('cascade_validation ' => true));) - See this

答案 1 :(得分:2)

没有足够的递归

foreach ($formFields as $key => $field) {
    if ($field->getErrorsAsString()) {
        $errors = $field->getErrors();
        foreach ($errors as $error) {
            $messages[$key][] = $error->getMessage();
        }
    }
}

试试这个 - 应该适用于所有嵌套错误:

class FormHelper
{
    public static function getErrorMessages($form)
    {
        $errors = array();
        if (!$form->isBound()) return $errors;
        foreach ($form->getErrors() as $key => $error) {
            $template = $error->getMessageTemplate();
            $parameters = $error->getMessageParameters();

            foreach ($parameters as $var => $value) {
                $template = str_replace($var, $value, $template);
            }

            $errors[$key] = $template;
        }
        if ($form->hasChildren()) {
            foreach ($form->getChildren() as $child) {
                if (!$child->isValid()) {
                    $name = $child->getConfig()->getOption('title', $child->getName());
                    $errors[$name] = self::getErrorMessages($child);
                }
            }
        }

        return $errors;
    }

}

答案 2 :(得分:0)

您没有显示错误消息

{{ form_errors(regForm.password.first) }}
{{ form_errors(regForm.password.second) }}