我有一个关于通过实体和验证配置文件进行symfony 2验证的问题。以下是对问题的解释。
我有一个用户帐户设置的实体,它有5个属性 - 子域名,电子邮件,oldPassword,密码和new_password_confirmed。
我的配置文件如下:
Backend\Builders\PageBundle\Entity\AccountSettings:
properties:
email:
- Email: ~
oldPassword:
- Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
message: "Wrong value for your current password"
constraints:
- Expression:
expression: "this.passwordMatch()"
message: "Passwords don`t match"
我的问题是 - 我如何使用此实体进行验证,并选择仅针对某个项目(例如电子邮件)进行验证。 以下是此验证的代码,但它需要满足所有验证规则才能使验证成功:
$accountSettings = new AccountSettings();
$accountSettings->email = $_POST['email'];
$validator = $this->get('validator');
$errors = $validator->validate($accountSettings);
答案 0 :(得分:2)
然后您应该使用validation groups。它允许你这样做,
Backend\Builders\PageBundle\Entity\AccountSettings:
properties:
email:
- Email: { groups: [xxx_group] }
oldPassword:
# ...
$errors = $validator->validate($accountSettings, array('xxx_group'));
检查the example中提供的the documentation。