Symfony2 test on ajax form



在symfony2应用程序中,我正在尝试测试一个表单,该表单具有动态添加的下拉列表(取决于以前的下拉列表选择)。

基本上我的问题是我无法设置需要保存在数据库中的下拉列表的值,因为爬虫不会看到它。

有人知道我应该怎么做吗?也许以某种方式在测试中创建表单元素"下拉列表"并设置其值?

这是我的代码:

    // Fill lesson create form
    $form = $crawler->selectButton('submit')->form();
    $lesson_name = 'Lesson_Test_'.time();
    $form['LessonDetailForm[name]'] = $lesson_name;
    $form['LessonDetailForm[description]'] = 'This is a test lesson.';
    $form['LessonDetailForm[text]'] = 'The content of this lesson is quite short.';
    // This worked on a non-dynamic dropdown list :
    //$availableOptionValues = $form['LessonDetailForm[topic]']->availableOptionValues();
    //$form['LessonDetailForm[topic]']->select($availableOptionValues[1]);
    //$form['LessonDetailForm[topic]'] = 1;
    // Here I would like to build the dropdown list myself:
    $form['LessonDetailForm[topic]'] = new Form(...);
    $crawler = self::$client->submit($form);

我还在考虑修改表单,特别是为了测试,有可能以某种方式不发布此下拉列表的值。但这似乎是一种不好的做法。

您的代码不会出现在 AJAX Symfony2 代码中。您的控制器中有完整的功能吗?你知道这个吗:

if( $request->isXmlHttpRequest() )

最新更新