Symfony 3 Override FosUserBundle验证程序

时间:2019-03-06 14:29:22

标签: symfony fosuserbundle symfony-3.4 symfony-validator

我能够覆盖FosUserBundle模板和控制器,但不能覆盖注册表单验证器。 但似乎验证已禁用。因为即使原始验证文件说密码需要8个字符,我也可以使用一个字符的密码注册新用户,而我不需要。

config.yml:

fos_user:
    registration:
        form:
            type: loic\UserBundle\Form\RegistrationType
            name:               form_register_new
            validation_groups:  [Registration, Default]
        confirmation:
            enabled:    false 
            template:   FOSUserBundle:Registration:email.txt.twig

RegistrationType.php

    <?php

namespace loic\UserBundle\Form;
use KMS\FroalaEditorBundle\Form\Type\FroalaEditorType;
use loic\EditorBundle\Form\EditorType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use loic\ContentBundle\loicContentBundle;
use loic\ContentBundle\Entity\Content;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;

class RegistrationType extends AbstractType

{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class,
                'options' => array(
                    'translation_domain' => 'FOSUserBundle',
                    'attr' => array(
                        'autocomplete' => 'new-password',
                    ),
                ),
                'first_options' => array('label' => 'Mot de passe'),
                'second_options' => array('label' => 'Confirmer le mot de passe'),
                'invalid_message' => 'fos_user.password.mismatch',

            ))


            ->add('email',null,array(
                'data' => time().'@gmail.com',
                'label' => 'E-mail'
            ));

    }


    public function getName()

    {
    return $this->getBlockPrefix ();
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
                        "allow_extra_fields" => true

        ));
    }

}

感谢帮助!

1 个答案:

答案 0 :(得分:0)

您的新“密码”字段没有约束。要测试验证,请添加约束,然后重试注册:

$builder->add('plainPassword', 'Symfony\Component\Form\Extension\Core\Type\RepeatedType', array(
    'type' => 'Symfony\Component\Form\Extension\Core\Type\PasswordType',
    'options' => array('translation_domain' => 'FOSUserBundle'),
    'first_options' => array('label' => 'form.password'),
    'second_options' => array('label' => 'form.password_confirmation'),
    'invalid_message' => 'fos_user.password.mismatch',
    'constraints' => array(
         new Length(['min' => 10]),
    )
));