我的validation.yml中为我的用户定义了验证约束 实体,但实际上并没有被调用;它正在努力 一点,但现在没有检查三个约束。 我得到SQL唯一约束错误,所以它肯定不存在 在代码中妥善处理。
这是我的validation.yml:
Acme\CPBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: username, message: userExists }
- FOS\UserBundle\Validator\Unique: {property: usernameCanonical, message: userExists}
- Callback:
methods: [isTimeZoneValid]
properties:
firstName:
- MaxLength: 255
lastName:
- MaxLength: 255
username:
- NotBlank: { groups: [create_user] }
- MinLength: { limit: 4, groups: [create_user] }
- MaxLength: { limit: 50, groups: [create_user] }
email:
- NotBlank: { groups: [create_user, edit_user] }
- MaxLength: { limit: 255, groups: [create_user, edit_user] }
- Email:
message: The email "{{ value }}" is not a valid email
checkMX: true
plainPassword:
- NotBlank: { groups: [create_user] }
- MinLength: { limit: 6, groups: [create_user], message: "passwordMin {{ limit }}" }
newPassword:
- MinLength: { limit: 6, groups: [edit_user], message: "passwordMin {{ limit }}" }
singleRole:
- Choice: { callback: getAllowedRoles }
这是我的用户实体:
<?php
namespace Acme\CPBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Acme\CPBundle\Constant\SecurityConstant;
use Symfony\Component\Validator\ExecutionContext;
/**
* Acme\CPBundle\Entity\User
*/
class User extends BaseUser {
/**
* @var \Acme\CPBundle\Entity\Organization $organization
*/
private $organization;
/**
* @var string $firstName
*/
private $firstName;
/**
* @var string $lastName
*/
private $lastName;
/**
* @var DateTime $timeZone
*/
private $timeZone;
/**
* @var Role $singleRole
*/
private $singleRole;
/**
* @var ArrayCollection $sessions
*/
private $sessions;
/**
* @var integer $id
*/
protected $id;
public function __construct() {
parent::__construct();
$this->enabled = true;
$this->sessions = new ArrayCollection();
}
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
public function getFirstName() {
return $this->firstName;
}
public function setLastName($lastName) {
$this->lastName = $lastName;
}
public function getLastName() {
return $this->lastName;
}
public function setTimeZone($timeZone) {
$this->timeZone = $timeZone;
}
public function getTimeZone() {
return $this->timeZone;
}
public function getId() {
return $this->id;
}
public function setOrganization(\Acme\CPBundle\Entity\Organization
$organization) {
$this->organization = $organization;
}
public function getOrganization() {
return $this->organization;
}
public function setSingleRole($singleRole) {
$this->singleRole = $singleRole;
}
public function getSingleRole() {
return $this->singleRole;
}
public function setSessions($sessions) {
$this->sessions = $sessions;
}
public function getSessions() {
return $this->sessions;
}
/*
* Form Validation Methods
*/
public static function getAllowedRoles() {
return array(SecurityConstant::ROLE_ADMIN,
SecurityConstant::ROLE_ORG_ADMIN, SecurityConstant::ROLE_USER);
}
public function isTimeZoneValid(ExecutionContext $context) {
$timeZoneIdentifiers = \DateTimeZone::listIdentifiers();
if (!in_array($this->getTimeZone(), $timeZoneIdentifiers)) {
$propertyPath = $context->getPropertyPath() . '.timeZone';
$context->setPropertyPath($propertyPath);
$context->addViolation('timeZoneInvalid', array(), null);
}
}
我一直试图弄清楚这几天,但我没有丝毫 关于问题可能是什么的想法。
任何帮助都将不胜感激。
谢谢,
答案 0 :(得分:4)
显然,您也可以像任何其他约束一样设置组。
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: username, groups: [create_user, default], message: userExists }
- FOS\UserBundle\Validator\Unique: { property: usernameCanonical, groups: [create_user, default], message: userExists }
- Callback:
methods: [isTimeZoneValid]
groups: [create_user, default]