カテゴリー
diary

Symfony2 with custom validator

昨日の投稿の後にちゃっかり自分でカスタムバリデート作っているコード見つけたので一応ここに貼り付けておく。symfonyの公式ドキュメントにもそのまんま記載してある。

ただこれだとヤヤ面倒くさめなのでAssert\Trueなんかの手軽さでAssertのエラーメッセージをカスタマイズしたい!

[php]
<?php
// src/Hc100/DemoBundle/Validator/constraints/ExistsDepartmentNumber.php

namespace Hc100\DemoBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
class ExistsDepartmentNumber extends Constraint
{
public $message = ‘部門番号 "%string%" が部門マスタに登録されていません’;

public function validatedBy()
{
return ‘existsdepartmentnumber’;
}
}
[/php]

validatedByには src/Hc100/DemoBundle/Resources/config/services.xml に定義してあるserviceを返す

[php]
<?php
// src/Hc100/DemoBundle/Validator/constraints/ExistsDepartmentNumberValidator.php

namespace Hc100\DemoBundle\Validator\Constraints;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Hc100\DemoBundle\Entity\Department;

class ExistsDepartmentNumberValidator extends ConstraintValidator
{
private $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function validate($value, Constraint $constraint)
{
if ($value == ”)
return;

$department = $this->entityManager->getRepository(‘Hc100DemoBundle:Department’)
->findMatchByDepartmentNumber($value);
if (!$department) {
$this->context->addViolation($constraint->message, array(‘%string%’ => $value));
}
}
}
[/php]

src/Hc100/DemoBundle/Resources/config/services.xmlにサービスを追記

[xml]
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="hc100_validator.constraint.existsdepartmentnumber" class="Hc100\DemoBundle\Validator\Constraints\ExistsDepartmentNumberValidator">
<argument type="service" id="doctrine.orm.entity_manager"/>
<tag name="validator.constraint_validator" alias="existsdepartmentnumber" />
</service>

</services>
</container>
[/xml]

あとはEntityの中でカスタムAssertを追加

[php]
use Hc100\DemoBundle\Validator\Constraints as Hc100Assert;

/**
* 部門
* @var string $department_number
*
* @ORM\Column(name="department_number", type="integer")
* @Hc100Assert\ExistsDepartmentNumber()
*/
private $department_number;
[/php]