3

我正在Symfony 3.2上构建应用程序

在应用程序的一个部分,我为我的用户提供了一个界面来更改他们的密码。对于这个任务,我有一个简单的表单,它绑定到我的ChangePasword实体,如下所示。

表格类:

namespace MasterBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChangePasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('oldPassword', PasswordType::class)
            ->add('newPassword', PasswordType::class);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'MasterBundle\Entity\ChangePassword'
            )
        );
    }

    public function getBlockPrefix()
    {
        return 'change_password';
    }
} 

和模型:

<?php

namespace MasterBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;

class ChangePassword
{

    /**
     * @UserPassword(message="Your current password is not correct.")
     */
    private $oldPassword;

    /**
     * @Assert\NotBlank(message="New password can not be blank.")
     * @Assert\Regex(pattern="/^(?=.*[a-z])(?=.*\\d).{6,}$/i", message="New password is required to be minimum 6 chars in length and to include at least one letter and one number.")
     */
    private $newPassword;

// other setter and getter stuff.
}

现在,问题是正则表达式验证器不起作用。它与任何东西都不匹配。

但是,如果我修改模型如下;它完美无缺:

<?php

namespace MasterBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class ChangePassword
{

    private $oldPassword;

    private $newPassword;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
    $metadata->addPropertyConstraint('oldPassword', new Userpassword(array(
        'message' => 'Your current password is not correct.',
    )));

    $metadata->addPropertyConstraint('newPassword', new Assert\NotBlank(array(
        'message' => 'New password can not be blank.',
    )));

    $metadata->addPropertyConstraint('newPassword', new Assert\Regex(array(
        'pattern' => '/^(?=.*[a-z])(?=.*\\d).{6,}$/i',
        'message' => 'New password is required to be minimum 6 chars in length and to include at least one letter and one number.'
    )));
}

// other setter and getter stuff.
}

您对这个问题的根源有任何想法吗?任何关于进一步调试案例的想法,将不胜感激。

4

1 回答 1

2

似乎对于这个特定的正则表达式,注释中的那个不应该转义\d位,但是在 PHP 实现中(即 using loadValidatorMetadata)我们需要转义它。

如果我按如下方式编辑在注释中实现约束的模型,它将起作用:

/**
 * @Assert\NotBlank(message="New password can not be blank.")
 * @Assert\Regex(pattern="/^(?=.*[a-z])(?=.*\d).{6,}$/i", message="New password is required to be minimum 6 chars in length and to include at least one letter and one number.")
 */
private $newPassword;

只需编辑/^(?=.*[a-z])(?=.*\\d).{6,}$/i/^(?=.*[a-z])(?=.*\d).{6,}$/i

这似乎不一致。我认为注释、yaml、xml 和 PHP 实现需要尽可能相同,文档应该强调所有的不一致。

因此我在 Symfony repo 上提出了一个问题。

于 2017-05-23T07:01:41.987 回答