我在我的用户实体的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);
}
}
几天来我一直试图解决这个问题,但我对问题可能是什么一无所知。
任何帮助,将不胜感激。
谢谢,