【发布时间】:2015-03-20 05:23:57
【问题描述】:
我的实体:
<?php
namespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* PriceQuoteRequest
*
* @ORM\Table(name="pricequote")
* @ORM\Entity
*
* @Assert\Callback(methods={"isGutterValid"}, groups={"flow_dormerRequest_step2"})
*/
class PriceQuoteRequest
{
/**
* @var integer $dormerInnerHeight
*
* @ORM\Column(name="h", type="float")
* @Assert\NotBlank(groups={"flow_dormerRequest_step2"})
* @Assert\Range(min="80", max="1000", groups={"flow_dormerRequest_step2"})
*/
public $dormerInnerHeight;
/**
* @var boolean $dormerGutter
*
* @ORM\Column(name="gutter", type="boolean")
*
* @Assert\Type(type="boolean", groups={"flow_dormerRequest_step2"})
* Assert\Callback(methods={"isGutterValid"}, groups={"flow_dormerRequest_step2"})
*/
public $dormerGutter;
/**
* @ORM\OneToOne(targetEntity="Acme\AppBundle\Entity\GutterMaterial")
*
* Assert\Type(type="Acme\AppBundle\Entity\GutterMaterial", groups={"flow_dormerRequest_step2"})
*/
public $dormerGutterMaterial;
/**
*
* @param ExecutionContextInterface $context
*
* Assert\Callback(groups={"flow_dormerRequest_step2"})
*/
public function isGutterValid(ExecutionContextInterface $context)
{
if ($this->getDormerGutter() === true && $this->getDormerGutterMaterial() === null) {
$context->buildViolation('Gutter checked but no Material selected.')
->atPath('dormerGutter')
->addViolation();
}
}
}
我的测试:
<?php
namespace Acme\AppBundle\Tests\Entity;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Validation;
class RequestFunctionalTest extends KernelTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
public function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager()
;
}
public function testDormerGutterValidator()
{
$priceQuoteRequest = new PriceQuoteRequest();
$priceQuoteRequest->setDormerGutter(true);
// Validating on single property will not call "isGutterValid" Callback, exptected only 1 error otherwise
// $violationList = $this->getValidator()->validateProperty($priceQuoteRequest, 'dormerGutter', array('flow_dormerRequest_step2'));
// Validating full object will call "isGutterValid" but return all errors resp. > 1
$violationList = $this->getValidator()->validate($priceQuoteRequest, array('flow_dormerRequest_step2'));
dump($violationList);
$this->assertEquals(1, count($violationList));
}
}
如果 DormerGutter 设置为 true(分别签入表单),则必须选择 DormerGutterMaterial。
isGutterValid 方法应该检查这一点,否则将违规指向 DormerGutter 字段:
if ($this->getDormerGutter() === true && $this->getDormerGutterMaterial() === null) {
$context->buildViolation('Gutter checked but no Material selected.')
->atPath('dormerGutter')
->addViolation();
}
测试将 DormerGutter 设置为 true,但未选择 DormerGutterMaterial。 验证属性 DormerGutter 我希望得到 isGutterValid 回调返回的 1 个错误。
相反,它将返回 0 个错误,因为在单个属性上调用 validateProperty 不会调用整个对象的“全局”回调。类。
相反,我可以验证完整的对象。这确实会调用 isGutterValid 方法。 它将返回至少 2 个错误,因为这也验证了所有其他属性,例如dormerInnerHeight 不幸的是 - 在我的用例中 - 我只希望返回 1 个错误。
有没有办法将回调指向单个属性,仅使我能够调用 validateProperty 方法并仍然触发回调?
替代/解决方法当然是使用表达式约束:
/**
* @var boolean $dormerGutter
*
* @ORM\Column(name="gutter", type="boolean")
*
* @Assert\Type(type="boolean", groups={"flow_dormerRequest_step2"})
* @Assert\Expression(
* "this.getDormerGutterMaterial() !== null or this.getDormerGutter() == 0",
* message="Gutter checked but no Material selected.",
* groups={"flow_dormerRequest_step2"}
* )
*/
public $dormerGutter;
Ps.:所有验证组都成功链接到flow_dormerRequest_step2。
GitHub Gist 上的完整代码here
【问题讨论】:
标签: validation symfony callback