简体   繁体   English

根据用户交互自定义Symfony表单

[英]Customize Symfony Form based on user interaction

I have AdsList Entity 我有AdsList实体

I want to add it to a CategoryAd Entity that is being the sub Category of MainCategory ( i hope that makes sense ) 我想将其添加到作为MainCategory的子类别的CategoryAd Entity中(我希望这很有意义)

I want this to be based on Dynamic Generation for Submitted Forms so when the use selects One of the MainCategories the next field should be populated with it's subcategories 我希望这是基于“ 提交表单的动态生成”的,因此当用户选择“ MainCategories”之一时,下一个字段应填充为其子类别

But i still don't know how to adjust that example for my case 但我仍然不知道如何针对我的情况调整该示例

if this is too much code i will make a gist 如果代码太多,我会提出要点

AdsList AdsList

class AdsList
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string")
 */
protected $title;

/**
 * @ORM\Column(type="text")
 */
protected $content;

/**
 * @ORM\ManyToOne(targetEntity="CategoryAd", inversedBy="ad")
 */
protected $category;

/**
 * @ORM\column(type="date", name="posted_at")
 */
protected $postedAt;

/**
 * @ORM\ManyToOne(targetEntity="Agency", inversedBy="ads")
 * @ORM\JoinColumn(name="agency_id", referencedColumnName="id")
 */
protected $postedBy;
}

CategoryAd CategoryAd

class CategoryAd
{
/**
 * @var int
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="name", type="string", length=255, unique=true)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="AdsList", mappedBy="category")
 */
private $ad;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\MainCategory", inversedBy="subCat")
 */
private $parentCat;

/**
 * CategoryAd constructor.
 */

public function __construct()
{
    $this->ad = new ArrayCollection();
}

MainCategory 主要类别

class MainCategory
{
/**
 * @var int
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="name", type="string", length=255, unique=true)
 */
protected $name;

/**
 * @var
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\CategoryAd", mappedBy="parentCat")
 * @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
 */
protected $subCat;

/**
 * MainCategory constructor.
 * @param string $name
 */
public function __construct($name)
{
    $this->name = $name;
}

This is the form 这是表格

at the moment it works, the thing is i wanted to do the Parent category because i have 90 categories at the moment, and it's only gonna get more 目前,我想做父类别,因为我目前有90个类别,而且只会得到更多

i dont know ajax but symfony does show an example in the documentation 我不知道ajax,但symfony确实在文档中显示了一个示例

can you help ? 你能帮我吗 ?

    /**
    * @Route("/post", name="post")
    */
    public function newAction(Request $request)
    {
    // create a Post and give it some dummy data for this example
    $task = new AdsList();
    $task->setpostedAt(new \DateTime('now'));

    $form = $this->createFormBuilder($task)
        ->add('title', TextType::class)
        ->add('content',TextareaType::class)
        ->add('category', EntityType::class, array(
        // query choices from Category.Name
            'class' => 'AppBundle:CategoryAd',
            'choice_label' => 'name',
        ))
        ->add('postedAt', DateType::class, array(
            'widget' => 'single_text',
            // this is actually the default format for single_text
            'format' => 'yyyy-MM-dd',
        ))
        ->add('save', SubmitType::class, array('label' => 'Create Post'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // $form->getData() holds the submitted values iN MeM
        $task = $form->getData();

        // save the task to the database
        $em = $this->getDoctrine()->getManager();
        $em->persist($task);
        $em->flush();
        return $this->redirectToRoute('list');
    }

    return $this->render('postlist/post.html.twig', array(
        'adForm' => $form->createView(),
    ));
}

You can't use your link Dynamic Generation for Submitted Forms for your use case. 您不能在用例中使用链接“ 动态生成提交的表单 ”。 The documentation of symfony only speak about generate dynamically a FormType not values. symfony的文档只讲动态生成FormType而不是值。

You should use javascript to populate data. 您应该使用javascript填充数据。 I don't know how is your FormType and your next field but you can create an ajax call that retrieve subcategories each time you select a main category and populate your field correctly. 我不知道您的FormType和下一个字段如何,但是您可以创建一个ajax调用,该调用在每次选择主类别并正确填充字段时都检索子类别。 If your use a CollectionType you just have to use your prototype. 如果使用CollectionType,则只需使用原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM