我对 Symfony 比较陌生,尝试寻找此问题的解决方案,但没有找到任何解决方案。我试图让用户(在前端)将表单字段添加到表单中。
我正在制作一个应用程序,用户可以在其中发布食谱,并且用户应该能够将成分表单字段添加到表单中,我知道如何在纯 html 和 jQuery 中执行此操作,但我不知道如何执行此操作可以在 Symfony 中管理它。
这就是我目前构建表单的方式:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text');
$builder->add('email', 'email');
$builder->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
$builder->add('address', new AddressType());
$builder->add('Registreer', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\User'
));
}
public function getName()
{
return 'user';
}
}
这是我想要看到的结果,我怎样才能在 Symfony 中实现这样的效果?
HTML
<a href="#" id="add">Add ingredient</a>
<form>
<p><input type="text" name="ingredient[]" /></p>
</form>
jQuery
$(function(){
console.log('ready')
$('#add').click(function(){
$('form').append('<p><input type="text" name="ingredient[]" /><a href="#" class="remove">Remove</a></p>');
})
$('form').on('click', '.remove', function(){
$(this).parent().remove();
})
})
JSFiddle
最佳答案
当然。它们被称为集合:
http://symfony.com/doc/current/cookbook/form/form_collections.html
您应该在我的一篇帖子中找到您需要的几乎所有内容:
Form with a collection same entity type
如果您有任何问题/需要帮助,我非常了解集合类型,很乐意为您提供帮助。
关于jquery - Symfony 在前端添加表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505880/