Mat*_*der 5 forms validation symfony
在Symfony2中,我使用自定义约束来验证表单上的某些数据,但我想知道是否可以从表单中引入一个值来验证其他值?
这是我的约束......
<?php
// src\BizTV\ContainerManagementBundle\Validator\Constraints\ContainerExists.php
namespace BizTV\ContainerManagementBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class ContainerExists extends Constraint
{
public $message = 'Namnet är upptaget, vänligen välj ett annat.';
public function validatedBy()
{
return 'containerExists';
}
}
Run Code Online (Sandbox Code Playgroud)
我的验证员......
<?php
// src\BizTV\ContainerManagementBundle\Validator\Constraints\ContainerExistsValidator.php
namespace BizTV\ContainerManagementBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManager as EntityManager;
class ContainerExistsValidator extends ConstraintValidator
{
private $container;
private $em;
public function __construct(Container $container, EntityManager $em) {
$this->container = $container;
$this->em = $em;
}
public function isValid($value, Constraint $constraint)
{
$em = $this->em;
$container = $this->container;
$company = $this->container->get('security.context')->getToken()->getUser()->getCompany();
//Fetch entities with same name
$repository = $em->getRepository('BizTVContainerManagementBundle:Container');
$query = $repository->createQueryBuilder('c')
->where('c.company = :company')
->setParameter('company', $company)
->orderBy('c.name', 'ASC')
->getQuery();
$containers = $query->getResult();
foreach ($containers as $g) {
if ($g->getName() == $value) {
$this->setMessage('Namnet '.$value.' är upptaget, vänligen välj ett annat', array('%string%' => $value));
return false;
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
通过服务使用......
services:
biztv.validator.containerExists:
class: BizTV\ContainerManagementBundle\Validator\Constraints\ContainerExistsValidator
arguments: ['@service_container', '@doctrine.orm.entity_manager']
tags:
- { name: validator.constraint_validator, alias: containerExists }
Run Code Online (Sandbox Code Playgroud)
这是我如何将它像这样应用到我的实体......
<?php
namespace BizTV\ContainerManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use BizTV\ContainerManagementBundle\Validator\Constraints as BizTVAssert;
use BizTV\UserBundle\Entity\User as user;
use BizTV\ContainerManagementBundle\Entity\Container as Container;
/**
* BizTV\ContainerManagementBundle\Entity\Container
*
* @ORM\Table(name="container")
* @ORM\Entity
*/
class Container
{
/**
* @var integer $id
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $name
* @Assert\NotBlank(message = "Du måste ange ett namn")
* @BizTVAssert\ContainerExists
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
Run Code Online (Sandbox Code Playgroud)
在我的验证器中,我希望能够完成这样的事情
public function isValid($FORM->OTHERVALUE, $value, Constraint $constraint)
{
$em = $this->em;
$container = $this->container;
$company = $this->container->get('security.context')->getToken()->getUser()->getCompany();
//Fetch entities with same name
$repository = $em->getRepository('BizTVContainerManagementBundle:Container');
$query = $repository->createQueryBuilder('c')
->where('c.company = :company')
->setParameter('company', $company)
->orderBy('c.name', 'ASC')
->getQuery();
$containers = $query->getResult();
if ($g->getName() == $FORM->OTHERVALUE) {
$this->setMessage('Namnet '.$value.' är upptaget, vänligen välj ett annat', array('%string%' => $value));
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Joã*_*ves 12
您可以创建一个类约束验证器(http://symfony.com/doc/master/cookbook/validation/custom_constraint.html#class-constraint-validator),然后通过它可以获得对象本身.
首先,您需要创建一个Constraint类:
class ContainerExists extends Constraint
{
public $message = 'Namnet är upptaget, vänligen välj ett annat.';
public function validatedBy()
{
return 'containerExists';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
Run Code Online (Sandbox Code Playgroud)
之后创建验证器本身,您可以访问该对象,而不仅仅是单个属性.
class ContainerExistsValidator extends ConstraintValidator {
private $em;
private $security_context;
public function __construct(EntityManager $entityManager, $security_context) {
$this->security_context = $security_context;
$this->em = $entityManager;
}
public function validate($object, Constraint $constraint) {
// do whatever you want with the object, and if something is wrong add a violation
$this->context->addViolation($constraint->message, array('%string%' => 'something'));
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建服务以访问实体管理器和东西:
validator.container_exists:
class: YourBundleName\Validator\Constraints\ContainerExistsValidator
arguments: ["@doctrine.orm.entity_manager", "@security.context"]
tags:
- { name: validator.constraint_validator, alias: containerExists }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,由于类约束验证器应用于类本身,而不是属性,因此需要将以下注释添加到类中.
use YourBundleName\Validator\Constraints as BackendAssert;
/**
* @BackendAssert\ContainerExists
*/
Run Code Online (Sandbox Code Playgroud)