Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将变量从表单子项传递到父项symfony?_Symfony_Twig - Fatal编程技术网

如何将变量从表单子项传递到父项symfony?

如何将变量从表单子项传递到父项symfony?,symfony,twig,Symfony,Twig,我在使用Symfony3表单时遇到问题 我定义的第一种形式如下: class MyForm1Type extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', MyForm2T

我在使用Symfony3表单时遇到问题

我定义的第一种形式如下:

class MyForm1Type extends AbstractType {
      /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', MyForm2Type::class, [
                'startDate' => 6,
            ])
        ;
    }
}
$builder
            ->add('name', MyForm2Type::class, [
                'attr' => [ startDate => 6 ],
            ])
        ;
第二种形式:

class MyForm2Type extends AbstractType
{
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'startDate' => null,
            'attr' => [
                'startDate' => ?????,
            ],
        ]);
    }

    /**
     * @return string|null
     */
    public function getParent()
    {
        return DateType::class;
    }

    /**
     * @return string|null
     */
    public function getBlockPrefix()
    {
        return 'datePicker';
    }
我会在configureOptions函数中获取fieldOption值并将其添加到attr数组中,这可能吗?我在表单主题中需要此值,以便使用动态startDate参数实例化datePicker,如下所示:

$('.input-datepicker').datepicker({
     startDate: "-{{ attr.startDate }}y",
});

提前感谢:)

解决方案很简单:D

我们只需在第一种形式中添加attr属性,如下所示:

class MyForm1Type extends AbstractType {
      /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', MyForm2Type::class, [
                'startDate' => 6,
            ])
        ;
    }
}
$builder
            ->add('name', MyForm2Type::class, [
                'attr' => [ startDate => 6 ],
            ])
        ;