1
我在我的validation.yml中爲我的用戶 實體定義了驗證約束,但實際上沒有一個實際被調用;它在 工作一點,但現在三個約束都沒有被檢查。 我得到SQL唯一約束錯誤,所以它絕對不是 在代碼中正確處理。FOSUserBundle/Symfony2:對用戶實體沒有檢查驗證約束
這裏是我的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);
}
}
我一直在試圖算出這個數天,但我沒有絲毫的想法 問題可能是什麼。
任何幫助,將不勝感激。
謝謝,
謝謝!在我找到這個之前花了我一個小時的搜索和嘗試。 – Andrew 2013-09-20 00:38:08
有同樣的問題。你有沒有把這個添加到validation.yml中 – 2014-06-25 15:16:44