使用silex 2.0和symfony 2.8表单组件(不是sf2完整堆栈),我有一个包含多个字段和嵌入表单的表单。
提交表单时,出现错误:
Warning: Invalid argument supplied for foreach() in vendor/symfony/form/Extension/Validator/Constraints/FormValidator.php on line 66
没有为任何字段和形式设置约束,但是,在FormValidator.php中,执行$config->getOption('constraints')
时,它会返回空字段,但 {{1}对于嵌入表格。
是否还要将约束设置为嵌入表单的空数组?如果我们必须手动设置它,我应该在哪里做?
以下是代码:
NULL
如果我删除class DebugPreviewChannelForm extends AbstractType
{
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function build(array $data, array $apps)
{
$builder = $this->formFactory->createBuilder(FormType::class, $data);
$builder->add(
'applicationId',
ChoiceType::class,
[
'choices' => $apps,
'label' => 'Application',
'choices_as_values' => true,
]
)
->add('user', UserType::class);
return $builder->getForm();
}
}
class UserType extends FormType implements DataMapperInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setCompound(true)
->setDataMapper($this)
->add(
'hash',
TextType::class,
['required' => false]
);
}
}
,则效果非常好。
答案 0 :(得分:0)
我终于成功了。我刚刚更改了userType类的实现。
来源:http://symfony.com/doc/current/cookbook/form/inherit_data_option.html
class UserType extends \Symfony\Component\Form\AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'hash',
TextType::class,
['required' => false]
);
}
}