我正在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.
}
您对这个问题的根源有任何想法吗?任何关于进一步调试案例的想法,将不胜感激。