0
我正在致力於Symfony,我有一個關係:產品 - OneToMany - 圖片。 我想在我的添加產品表格中,添加4圖片。Symfony - 在表格中添加多個表格
我知道如何用JavaScript做它(通過動態地添加與模式輸入...),但我想這樣做在我的形式,但我不知道怎麼...
這裏是我的代碼:
ProductType.php
class ProjectType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array('label' => 'Titre'))
->add('url', UrlType::class, array('label' => 'URL'))
->add('client', TextType::class, array('label' => 'Client'))
->add('description', TextType::class, array('label' => 'Description'))
->add('category', EntityType::class, array(
'class' => 'AntoinePortfolioBundle:Category',
'choice_label' => 'name',
'multiple' => false,
'label' => 'Catégories'))
->add('tags', EntityType::class, array(
'class' => 'AntoinePortfolioBundle:Tag',
'choice_label' => 'name',
'multiple' => true,
'label' => 'Tags'))
->add('images', CollectionType::class, array(
'entry_type' => ImageType::class));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Antoine\PortfolioBundle\Entity\Project'
));
}
}
ImageType.php
<?php
namespace Antoine\PortfolioBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('label' => 'Nom'))
->add('alt', TextType::class, array('label' => 'alt'))
->add('file', FileType::class, array('label' => 'Image'));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Antoine\PortfolioBundle\Entity\Image'
));
}
}
我的關係工作,這不是問題。
謝謝你的時間!
謝謝,但我已經寫了一個JavaScript腳本,實際上它工作得很好。 感謝這個鏈接,我會檢查出來! – Antoine