我的表格遇到问题,我需要帮助...
我想创建一个可以保留广告的表单。 我的问题来自我的ManyToMany项目'images'。 我通过'images'形式获得的新div看起来像这样:
<div><label class="required">Images</label><div id="annonce_rent_images" data-prototype="<div><label class="required">__name__label__</label><div id="annonce_rent_images___name__"><div><label for="annonce_rent_images___name___url" class="required">Url</label><input type="url" id="annonce_rent_images___name___url" name="annonce_rent[images][__name__][url]" required="required" /></div><div><label for="annonce_rent_images___name___alt" class="required">Alt</label><input type="text" id="annonce_rent_images___name___alt" name="annonce_rent[images][__name__][alt]" required="required" /></div></div></div>"></div></div>
字段“url”&amp; “alt”被固定在sub-div的data-prototype属性中 我该如何解决这个问题?
以下是我的代码的一些示例,如果您需要完整的文件我可以发布它们。
FORMBUILDER:
$builder
->add('name', TextType::class)
->add('description', TextType::class)
->add('price', TextType::class)
->add('location', TextType::class)
->add('zipcode', TextType::class)
->add('images', CollectionType::class, array(
'entry_type' => 'LAMainBundle\Form\ImageType',
'allow_add' => true,
'allow_delete' => true
))
->add('save', SubmitType::class, array('label' => 'Save'))
;
IMAGEBUILDER:
$builder
->add('url', UrlType::class)
->add('alt', TextType::class)
;
CONTROLLER addRentAction:
$annonce = new AnnonceRent();
$form = $this->createForm('LAMainBundle\Form\AnnonceRentType');
return $this->render('LAMainBundle:Admin:addrent.html.twig', array(
'form' => $form->createView(),
));
答案 0 :(得分:1)
你需要一些javascript来添加字段,这里有一种方法可以轻松地使用jQuery:
<div>
<label class="required">Images</label>
<div id="annonce_rent_images" data-prototype="<div><label class="required">__name__label__</label><div id="annonce_rent_images___name__"><div><label for="annonce_rent_images___name___url" class="required">Url</label><input type="url" id="annonce_rent_images___name___url" name="annonce_rent[images][__name__][url]" required="required" /></div><div><label for="annonce_rent_images___name___alt" class="required">Alt</label><input type="text" id="annonce_rent_images___name___alt" name="annonce_rent[images][__name__][alt]" required="required" /></div></div></div>">
</div>
</div>
<!-- ... -->
<button id="add_image">Add image</button>
<script src="/js/jquery-2.2.1.min.js"></script>
<script>
function addEntry() {
var $container = $('#annonce_rent_images'),
$prototype = $container.data('prototype');
$prototype.replace(/__name__/g, $container.children('div').length);
$container.append($prototype);
}
addEntry(); // add a first field by default
$('#add_image').click(addEntry);
</script>