【发布时间】:2016-10-12 11:10:23
【问题描述】:
我想将某个字段的值传递给另一个字段的自定义约束(->在自定义验证器中使用它)
带有一些字段的表单:
...
->add('BsaKey', new \app\...\fieldTypes\RadioButtonType(), [
'choices' => [
...
],
'expanded' => true,
'multiple' => false,
...
])
->add('MeteringCodes', 'collection', [
'type' => new \app\...\formTypes\MeteringCodeType(),
'allow_add' => true,
'label' => false,
'options' => ['label' => $this->lang->get('MeteringCode.Caption')],
'constraints' => new \app\...\validators\NoIdenticMeteringCodes()
])
...
现在我需要将 BsaKey 的值传递给 MeteringCodeType 的自定义约束:
class MeteringCodeType extends \Symfony\Component\Form\AbstractType
{
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{
$builder->add('meteringCode', 'text', [
'...' => '...',
'constraints' => new \app\...\MeteringCodeConstraint(['param' => 'VALUE_OF_BsaKey'])
]);
}
}
我怎样才能做到这一点?
附:我没有将 Symfony 作为一个整体来使用,只是一些独立的组件......
编辑:
谢谢,我找到了解决办法:
class MeteringCodeValidator extends \Symfony\Component\Validator\ConstraintValidator
{
public function validate($value, \Symfony\Component\Validator\Constraint $constraint)
{
$BsaKey = $this->context->getRoot()->get('BsaKey')->getData();
...
}
}
似乎独立于“getTargets()”函数返回的选项。
【问题讨论】:
标签: php symfony symfony-forms