Symfony2:对ArrayCollection的测试给出了"Unreachable field"(无法访问的字段). [英] Symfony2: Test on ArrayCollection gives "Unreachable field"

查看:53
本文介绍了Symfony2:对ArrayCollection的测试给出了"Unreachable field"(无法访问的字段).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在添加ArrayCollection成员的表单的功能测试中,有以下语句:

In a functional test of a form to add members of an ArrayCollection there is this statement:

$ form ['client [member] [1] [fname]'] ='Benny';

$form['client[members][1][fname]'] = 'Benny';

该字段名称已通过DOM检查器验证.

The field name was verified with a DOM inspector.

此行的控制台输出为:

InvalidArgumentException: Unreachable field "members"

G:\Documents\workspace\sym\vendor\symfony\symfony\src\Symfony\Component\DomCrawler\Form.php:459
G:\Documents\workspace\sym\vendor\symfony\symfony\src\Symfony\Component\DomCrawler\Form.php:496
G:\Documents\workspace\sym\vendor\symfony\symfony\src\Symfony\Component\DomCrawler\Form.php:319
G:\Documents\workspace\sym\src\Mana\ClientBundle\Tests\Controller\ClientControllerTest.php:57

应该使用什么方法来测试ArrayCollection成员的添加?

What method should be used to test the addition of an ArrayCollection member?

    //link to trigger adding household member form
    $link = $crawler->selectLink('Add household member')->link();
    $crawler = $client->click($link);
    $form = $crawler->selectButton('Add client')->form();
    $form['client[members][1][fname]'] = 'Benny';
    $form['client[members][1][dob]'] = "3/12/1999";
    $crawler = $client->submit($form);
    $this->assertTrue($crawler->filter('html:contains("Client View Form")')->count() > 0);

推荐答案

我只是遇到了同样的问题,经过一番研究,我找到了一个对我有帮助的解决方案. 从集合"表单类型中删除字段的解决方案 这篇文章并不是您想要的,这是为了删除元素而不是添加新元素.但是原理是一样的.我创建了一个数组,而不是$form->setValues() ... $form->getPhpValues(),然后将其发布了

I just had the same issue and after a bit of research I found a solution which helped me. A solution to remove a field from a Collection form type This post is not exactly what you were looking for, this one is for removing an element and not adding new ones. But the principle is the same. What I did instead of $form->setValues() ... $form->getPhpValues() that I've created an array, and POSTed that

在下面的示例中,表单的configurations字段为Collection

In the example bellow, the configurations field of the form is a Collection

    $submitButton = $crawler->selectButton(self::BUTTON_ADD_APPLICATION);
    $form = $submitButton->form();
    $values = array(
        'Setup' => array(
            '_token' => $form['Setup[_token]']->getValue(),
            'name'   => 'My New Setup',
            'configurations' => array(
                0 => array(
                    'country' => 'HUN',
                    'value'   => '3',
                ),
                1 => array(
                    'country' => 'GBR',
                    'value'   => '0',
                ),
            ),
        ),
    );

    $client->request($form->getMethod(), $form->getUri(), $values);

希望有帮助!还要感谢sstok提供的原始解决方案!

Hope it helps! And thanks for sstok for the original solution!

这篇关于Symfony2:对ArrayCollection的测试给出了"Unreachable field"(无法访问的字段).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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