validation - symfony2 validate a form value using another form value -
in symfony2, use custom constraints validate data on form, wonder if can bring in 1 value form use in validating value?
here's constraint...
<?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'; } } and validator...
<?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 container; use doctrine\orm\entitymanager 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 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 $g) { if ($g->getname() == $value) { $this->setmessage('namnet '.$value.' är upptaget, vänligen välj ett annat', array('%string%' => $value)); return false; } } return true; } } used through service...
services: biztv.validator.containerexists: class: biztv\containermanagementbundle\validator\constraints\containerexistsvalidator arguments: ['@service_container', '@doctrine.orm.entity_manager'] tags: - { name: validator.constraint_validator, alias: containerexists } here's how apply entity...
<?php namespace biztv\containermanagementbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use biztv\containermanagementbundle\validator\constraints biztvassert; use biztv\userbundle\entity\user user; use biztv\containermanagementbundle\entity\container 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; in validator able accomplish instead
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 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; }
you can create class constraint validator (http://symfony.com/doc/master/cookbook/validation/custom_constraint.html#class-constraint-validator) , object itself.
first need create constraint class:
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; } } after create validator have access object , not single property.
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) { // whatever want object, , if wrong add violation $this->context->addviolation($constraint->message, array('%string%' => 'something')); } } } then create service access entity manager , stuff:
validator.container_exists: class: yourbundlename\validator\constraints\containerexistsvalidator arguments: ["@doctrine.orm.entity_manager", "@security.context"] tags: - { name: validator.constraint_validator, alias: containerexists } in case class constraint validator applied class itself, , not property need add following annotation class.
use yourbundlename\validator\constraints backendassert; /** * @backendassert\containerexists */
Comments
Post a Comment