[英]Symfony: How to test a form-field of type collection in a functional test without JavaScript?
I'm struggling for some time now with the collection form. 我现在正在用收集表格挣扎一段时间。 I have a collection with of at least 3 included forms.
我有一个包含至少3种包含表格的集合。 But because included forms are not rendered, I cannot fill them up in functional test.
但由于包含的表格没有呈现,我无法在功能测试中填写它们。
This is how the collection looks like 这就是集合的样子
->add('references', 'collection', array(
'type' => 'reference',
'allow_add' => true,
'constraints' => array(
new C\Count(array(
'min' => 3,
'minMessage' => 'You should specify at least 3 references.'
))
)
))
And when this form is render it looks like 当这个表单渲染它看起来像
<label class="required">References</label><div id="form_references"
data-prototype="here-is-the-rendered-prototype-of-embedded-form">
How could I force Symfony Form to render couple of embedded forms also, without using javascript, so that I could easily functional test them or use them on browser without enabled JS? 如何在不使用javascript的情况下强制Symfony Form渲染几个嵌入的表单,以便我可以轻松地对它们进行功能测试或在浏览器上使用它们而不启用JS?
One more thing, forms are not bind to Enity/Model but just represent a simple array. 还有一件事,表单没有绑定到Enity / Model,只是表示一个简单的数组。
Note: I don't have experience with function tests, but perhaps you can somehow manually add references to your form using the same method that Symfony suggests for pre-rendering a collection without using javascript prototyping .... 注意:我没有使用函数测试的经验,但也许您可以使用Symfony建议的预渲染集合的相同方法手动添加对表单的引用,而无需使用javascript原型....
http://symfony.com/doc/current/cookbook/form/form_collections.html http://symfony.com/doc/current/cookbook/form/form_collections.html
Inside of the test controller, you can manually add the number of reference fields you want by adding references to your entity. 在测试控制器内部,您可以通过向实体添加引用来手动添加所需的引用字段数。
class ApplicationController extends Controller
{
public function newAction(Request $request)
{
$app = new Application();
for ($i = 0; $i < 3; $i++) {
$app->getReferences()->add(array(
'name' => 'Michael ' . $i,
'phone' => '1 (555) 555-5555'
));
}
$form = $this->createForm(new ApplicationForm(), $app);
if ($request->getMethod() === "POST")
{
$form->handleRequest($request);
if ($form->isValid()) {
// Process and Save the information.
}
}
return $this->render('AppBundle::Application:new.html.twig', array(
'form' => $form->createView(),
));
}
}
Inside the template, you can loop through the references, and then render them. 在模板内部,您可以遍历引用,然后渲染它们。
{{ form_start(form) }}
<h3>References</h3>
<div class="references">
{% for reference in form.references %}
<p>
{{ form_row(reference.name) }}
{{ form_row(reference.phone) }}
{{ form_row(reference.body) }}
</p>
{% endfor %}
</div>
{{ form_end(form) }}
This method was taken directly from the cookbook ... and perhaps you can create a function in your test that will manually add the references. 这个方法直接来自cookbook ......也许您可以在测试中创建一个手动添加引用的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.