我正在Symfony2中创建一个表单。表单只包含一个book
字段,它允许用户在Books
实体列表中进行选择。我需要检查所选的Book
是否属于我控制器中的Author
。
public class MyFormType extends AbstractType
{
protected $author;
public function __construct(Author $author) {
$this->author = $author;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('book', 'entity', array('class' => 'AcmeDemoBundle:Book', 'field' => 'title');
}
// ...
}
我想检查,提交表单后,选定的Book
是由$author
在我的控制器:
public class MyController
{
public function doStuffAction() {
$author = ...;
$form = $this->createForm(new MyFormType($author));
$form->bind($this->getRequest());
// ...
}
}
不幸的是,我找不到任何方法来做这件事。我尝试创建一个自定义的验证器约束,正如在The Cookbook中所解释的,但是当我可以通过将验证器定义为服务来传递EntityManager
作为参数时,我不能将$author
从控制器传递到验证器约束。
class HasValidAuthorConstraintValidator extends ConstraintValidator
{
private $entityManager;
public function __construct(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint) {
$book = $this->entityManager->getRepository('book')->findOneById($value);
$author = ...; // That's the data I'm missing
if(!$book->belongsTo($author))
{
$this->context->addViolation(...);
}
}
}
这个解决方案可以是我正在寻找的那个,但是我的表单不绑定到一个实体,也不意味着是(我从getData()
方法获得数据)。
我的问题有解决办法吗?这一定是很常见的情况,但我真的不知道如何解决。
在Cerad的帮助下,我终于弄明白了。要注入需要从ConstraintValidator::validate()
方法访问的自定义参数,您需要在Constraint
中将它们作为选项传递。
public class HasValidAuthorConstraint extends Constraint
{
protected $author;
public function __construct($options)
{
if($options['author'] and $options['author'] instanceof Author)
{
$this->author = $options['author'];
}
else
{
throw new MissingOptionException("...");
}
}
public function getAuthor()
{
return $this->author;
}
}
并且,在ConstraintValidator中:
class HasValidAuthorConstraintValidator extends ConstraintValidator
{
private $entityManager;
public function __construct(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint) {
$book = $this->entityManager->getRepository('book')->findOneById($value);
$author = $this->constraint->getAuthor();
if(!$book->isAuthor($author))
{
$this->context->addViolation(...);
}
}
}
最后但同样重要的是,您必须将参数传递给Validator:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('book', 'entity', array(
'class' => 'AcmeDemoBundle:Book',
'field' => 'title',
'constraints' => array(
new HasValidAuthorConstraint(array(
'author' => $this->author
))
)
));
}
首先向约束添加setAuthor方法,然后调整validate方法。接下来的诀窍是确定最好的称呼。
不清楚您是如何将验证器绑定到您的书的。你在使用验证吗?还是在表单内部做一些事情?
嗯,我对Form/Validation组件不是很熟悉,但是你可以使用一个包含作者姓名/id的Hidden字段,并检查它是否相同:
class MyFormType extends AbstractType
{
protected $author;
public function __construct(Author $author) {
$this->author = $author;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('book', 'entity', array('class' => 'AcmeDemoBundle:Book', 'field' => 'title');
->add('author_name', 'hidden', array(
'data' => $this->author->getId(),
))
;
}
// ...
}
接受的答案不适合我使用Symfony框架版本2.1。我就是这样解决的。
class CustomConstraint extends Constraint
{
public $dependency;
public $message = 'The error message.';
}
class CustomConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint->dependency->allows($value)) {
$this->context->addViolation($constraint->message);
}
}
}
class CustomFormType extends AbstractType
{
private $dependency;
public function __construct(Dependency $dependency)
{
$this->dependency = $dependency;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field', 'type', array(
'constraints' => array(
new CustomConstraint(array('dependency' => $this->dependency))
)
));
}
}