在 symfony 中访问嵌套形式 symfony(孩子的孩子)的按钮 [英] Accesing button in nested form symfony (child of a child) in symfony

查看:27
本文介绍了在 symfony 中访问嵌套形式 symfony(孩子的孩子)的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的表格:

$form = $this->createForm(new NewsType(), $news)
            ->add('edit', SubmitType::class, array('label' => 'edit'))
            ->add('delete', SubmitType::class, array('label' => 'delete'))
            ->add('comments', CollectionType::class, array('entry_type'   => CommentType::class));

评论类型:

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('author', TextType::class)
        ->add('text', TextType::class)
        ->add('remove', SubmitType::class);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Comment'));
}

是否可以从 CommentType 访问删除按钮,以便在单击它删除评论条目时.一切都正确映射,我可以看到我的页面上显示的评论对象,但是当我使用 $form->get('remove') 我得到 "Child "remove" does not exist." 有没有可能这样做?

Is it possible to access remove button from CommentType so when its clicked to delete comment entry. Everything is mapped properly, i can see comment objects displayed on my page, but when i use $form->get('remove') i get "Child "remove" does not exist." Is it even possible to do this way?

推荐答案

您需要访问一个孙子做:

You need to access a grand grand child doing:

foreach ($form->get('comments') as $entry) {
    $toRemove = $entry->get('remove')-isClicked();
    // handle it ...
}

但是要单独提交它,您必须确保在您的视图中构建完整"的子表单:

But to submit it separately you must ensure that your building the "complete" child form in your view:

{{ form_start(form) }}
{% for child in form %}
    {% if 'news_comments' == child.vars['full_name'] %}
        {{ form_start(child) }}
        {{ form_row(child) }}
        {{ form_end(child) }}
    {% else %}
        {{ form_row(child) }}
    {% endif %}
{% endfor %}
{{ form_end(form) %}

附注:

小心,您似乎使用了 symfony 2.8 并更新了表单类型的 FQCN,但创建表单也需要它:

be careful, you seem to use symfony 2.8 and to have updated the FQCN for the form types, but it's needed too for creating the form:

$form = $this->createForm(NewsType::class, $news)

这篇关于在 symfony 中访问嵌套形式 symfony(孩子的孩子)的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆