Userエンティティのvalidation.ymlで検証制約を定義していますが、実際には呼び出されていません。ある時点では機能していましたが、現在は3つの制約のいずれもチェックされていません。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);
}
}
私はこれを何日も理解しようとしてきましたが、問題が何であるかについては少しもわかりません。
どんな助けでもいただければ幸いです。
ありがとう、