how to add validation to unbound form field in symfony2

I am trying to validate an unbound field in my symfony2 form.

I have googled it and found several similar solutions, so I did a copy-paste and put my own stuff in there, but it doesn't work for me. There is probably a use statement or something that was missing from all the solutions I read, omitted because everyone would know them to be necessary.. .except the rookie here that is.

My code:

<?php

namespace BizTVUserBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;

use SymfonyComponentValidatorConstraintsCallbackValidator as CallbackValidator;
use SymfonyComponentFormFormValidatorInterface;

use DoctrineORMEntityRepository;

class newServerType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {

        $builder
            ->add('company', 'entity', array(
                'label' => 'Vilket företag tillhör servern? ',
                'multiple' => FALSE,
                'expanded' => FALSE,
                'property' => 'companyName',
                'class'    => 'BizTVBackendBundleEntitycompany',
            ));     

        $builder->add('screenName', null, array('property_path' => false, 'label' => 'Namn på skärmen (användaren kan ändra det senare) '));

        $builder-> addValidator(new CallbackValidator(function(FormInterface $form){
          $screenName = $form['screenName'];
            if (empty($screenName)) {
              $form['screenName']->addError(new FormError("Du måste ange ett namn för den nya skärmen"));
            }
        }));

        $builder
            ->add('username', 'text', array('label' => 'Servernamn (endast för serverns login samt vår referens)'))            
            ->add('plainPassword', 'repeated', array('type' => 'password', 'first_name' => 'Lösenord för server ', 'second_name' => 'Upprepa lösenord för server',));

    }

    public function getName()
    {
        return 'biztv_userbundle_newservertype';
    }
}

My error:

Catchable Fatal Error: Argument 1 passed to SymfonyComponentFormFormBuilder::addValidator() must implement interface SymfonyComponentFormFormValidatorInterface, instance of SymfonyComponentValidatorConstraintsCallbackValidator given, called in /var/www/biztv_symfony/src/BizTV/UserBundle/Form/newServerType.php on line 35 and defined in /var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/Form/FormBuilder.php line 255


So, for anyone else needing validation for unbound form fields in symfony 2 here Is how I ended up doing it - turned out I was missing a bunch of use statements, they weren't listed on any of the sites where I found other threads on the subject.

I have weeded out everything but the essentials. Thanks to m2mdas for helping me along on this one.

<?php

namespace BizTVUserBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;
use SymfonyComponentFormFormInterface;
use SymfonyComponentFormCallbackValidator;
use SymfonyComponentFormFormValidatorInterface;
use SymfonyComponentFormFormError;


use DoctrineORMEntityRepository;

class newServerType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {   
        $builder->add('screenName', null, array('property_path' => false, 'label' => 'Namn på skärmen (användaren kan ändra det senare) '));

        $builder-> addValidator(new CallbackValidator(function(FormInterface $form){
          $screenName = $form->get('screenName')->getData();
            if (empty($screenName)) {
              $form['screenName']->addError(new FormError("Du måste ange ett namn för den nya skärmen"));
            }
        }));    
    }

    public function getName()
    {
        return 'biztv_userbundle_newservertype';
    }
}

CallbackValidator FQCN应该是SymfonyComponentFormCallbackValidator

链接地址: http://www.djcxy.com/p/57880.html

上一篇: Symfony2 AJAX验证

下一篇: 如何为symfony2中的未绑定表单字段添加验证