jquery - 如何在 Symfony2 中重写原型(prototype)渲染

标签 jquery symfony

如果我在表单中嵌入表单类型集合。我已将allow_add 设置为true。如何覆盖原型(prototype)输出?假设我想在每次添加时将表单类型的集合包装在一个表中。我想我可以使用 jquery 中的原型(prototype)输出,但更愿意从一开始就做到这一点。作为一个例子,我的收藏类型是:

class EmailShareType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder->add("shareName","text",array("label"=>"Recipient Name:"));
       $builder->add("emailName", "email", array("label"=>"Recipient Email:","required" => true));
       $builder->add("emailMessage", "textarea",array("label"=>"Enter email message", "max_length" => "4000"));
    }

    public function getName() 
    {
        return 'emailShare';
    }    
} 

最终的原型(prototype)呈现为:

data-prototype="
<div>
<label class="required">__name__label__</label>
<div id="emailShareCollection_emailShares___name__">
<div>
<label for="emailShareCollection_emailShares___name___shareName" class="required">Recipient Name:</label>
<input type="text" id="emailShareCollection_emailShares___name___shareName" name="emailShareCollection[emailShares][__name__][shareName]" required="required" />
</div>
<div>
<label for="emailShareCollection_emailShares___name___emailName" class="required">Recipient Email:</label>
<input type="email" id="emailShareCollection_emailShares___name___emailName" name="emailShareCollection[emailShares][__name__][emailName]" required="required" />
</div>
<div>
<label for="emailShareCollection_emailShares___name___emailMessage" class="required">Enter email message</label>
<textarea id="emailShareCollection_emailShares___name___emailMessage" name="emailShareCollection[emailShares][__name__][emailMessage]" required="required" maxlength="4000"></textarea>
</div>
</div>
</div>"

如何控制此输出?

最佳答案

您可以通过传递自定义模板来根据需要呈现它:

data-prototype="{% filter escape %}{% include 'YourBundle:YourController:yourTemplate.html.twig' with {'form': form.collectonField.get('prototype')} %}{% endfilter %}"

关于jquery - 如何在 Symfony2 中重写原型(prototype)渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946995/

相关文章:

javascript - 调用按钮单击功能时保持类 this

jquery - FOSJsRoutingBundle : Cannot Generate routing url inside javascript

php - 如何将命令的输出作为另一个命令的输入?

php - 使用 assetic 提供相关图像

symfony - 在 Symfony 项目中查找未使用的 TWIG 布局

javascript - 在滚动时修改高度时侧边栏无法控制地跳转

javascript - 使部分contentEditable无法删除

authentication - Twig 中的 app.user.username 返回无法添加全局

javascript - Jquery 具有 2 次点击功能

javascript - 逐步显示每个div