本文介绍了未绑定表单字段的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 'property_path' => false 添加的额外未绑定字段的表单.

I have a form with an extra unbound field added using 'property_path' => false.

我想对这个领域做一个简单的验证,我找到了很多建议使用类似

I would like to have a simple validation on this field and I've found many answers that suggest to use something like

$builder->addValidator(...);

但我已经看到在 symfony 2.1 中 $builder->addValidator 已被弃用.有谁知道在 Symfony 2.1 中对未绑定字段进行验证的正确方法是什么?

but I've seen that in symfony 2.1 $builder->addValidator is deprecated. Does anyone know what is the correct way to put a validation on an unbound field in Symfony 2.1?

推荐答案

我刚刚就主题做了一个更完整的回答 Symfony 使用映射的错误表单字段验证表单

验证表单中的未绑定(非映射)字段没有很好的文档记录,快速发展的表单和验证器组件使少数示例过时(对于 Symfony 2.1.2).

Validating unbound (non mapped) field in a form is not well documented and the fast evolving form and validator components make the few examples obsoletes (for Symfony 2.1.2).

现在我成功地使用事件侦听器验证了非映射字段.这是我的简化代码:

For now I succeed in validated non mapped fields with event listener.Here is my simplified code :

namespace Dj\TestBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Symfony\Component\Form\FormEvents;
use Dj\TestBundle\Form\EventListener\NewPostListener;

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('lineNumber', 'choice', array(
                        'label' => 'How many lines :',
                        'choices' => array(
                            3 => '3 lines',
                            6 => '6 lines'
                        ),
                        // 'data' => 3, // default value
                        'expanded' => true,
                        'mapped' => false
                    ))
                ->add('body', 'textarea', array(
                        'label' => 'Your text',
                        'max_length' => 120));

        // this listener will validate form datas
        $listener = new NewPostListener;
        $builder->addEventListener(FormEvents::POST_BIND, array($listener, 'postBind'));
    }

    // ... other methods
}

和事件监听器:

namespace Dj\TestBundle\Form\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormError;

/**
 * listener used at Post creation time to validate non mapped fields
 */
class NewPostListener
{

    /**
     * Validates lineNumber and body length
     * @param \Symfony\Component\Form\FormEvent $event
     */
    public function postBind(FormEvent $event)
    {
        $form = $event->getForm();
        $data = $event->getData();

        if (!isset($data->lineNumber)) {
            $msg = 'Please give a correct line number';
            $form->get('lineNumber')->addError(new FormError($msg));
        }

        // ... other validations

    }
}

这就是我验证非映射字段的方式,直到我找到如何使用验证器执行此操作.

This is how I validate my non mapped fields until I find out how to do this with validators.

这篇关于未绑定表单字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:30