Symfony 3 переопределить валидаторы FosUserBundle - PullRequest
0 голосов
/ 06 марта 2019

Я могу переопределить шаблоны и контроллеры 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 голосов
/ 06 марта 2019

Ваше новое поле «пароль» не имеет ограничений. Чтобы проверить валидацию, добавьте ограничение и повторите попытку регистрации:

$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]),
    )
));
...