我正在 Symfony 3.2
上构建应用程序在应用程序的一部分,我提供了一个界面,我的用户可以更改密码。对于此任务,我有一个简单的表单绑定到我的DocumentContext.EditedDocument
实体,如下所示。
表单类:
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.
}
你对这个问题的根源有什么看法吗?关于进一步调试案例的任何想法,将不胜感激。
答案 0 :(得分:1)
对于这个特定的正则表达式,注释中的那个似乎不应该转义\d
位,但是在PHP实现中(即使用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实现需要相同,尽管可能,documentation应该强调所有的不一致。
因此我在Symfony repo上raised an issue。