简体   繁体   中英

Customize Symfony Form based on user interaction

I have AdsList Entity

I want to add it to a CategoryAd Entity that is being the sub Category of MainCategory ( i hope that makes sense )

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

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

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

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

i dont know ajax but symfony does show an example in the documentation

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.

You should use javascript to populate data. 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. If your use a CollectionType you just have to use your prototype.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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