Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate same form type on same page multiple times Symfony2

I'm trying to generate a form type in particular the "ProductLanguageType".

I want to generate the ProductLanguageType as many times as the current numbers of existing languages in the Language table.

For example if I have (English, French, Russian, Chinese) in the Language table, it would generate 4 ProductLanguageType form on the same page.

I would like to know how do I query language table and generate multiple form of the same type on the same page, is the form builder capable of doing it or is there another workaround? Been having some trouble with this for some time now and would be happy to find a good solution for this.

ProductLanguageType:

class ProductLanguageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){

    $builder->add('id_language', 'entity', array(
                   'class' => 'AdminBundle:Language',
                   'data_class' => 'Main\AdminBundle\Entity\Language',
                   'property' => 'language'
                  )
                 )
            ->add('name', 'text')
            ->add('description', 'ckeditor', array(
                    'config_name' => 'admin',
                    'config' => array(
                        'filebrowser_image_browse_url' => array(
                            'route'            => 'elfinder',
                            'route_parameters' => array('instance' => 'default'),
                        ),
                    )
                ))
            ->add('short_description', 'text');

}

public function getName(){
    return 'productLanguage';
}
}

ProductType(ProductLanguageType is embeded in here):

class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){

    $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
    $builder->add('productLanguage', new ProductLanguageType())
            ->add('id_seller','text')
            ->add('price','text')
            ->add('cost_price','text')
            ->add('retail_price','text')
            ->add('hot', 'checkbox')
            ->add('featured', 'checkbox')
            ->add('new', 'checkbox')
            ->add('free_shipping', 'checkbox')
            ->add('status','text') //active or inactive, to be decided if hidden or visible
            ->add('Add', 'submit');

}
}
like image 514
Karl Wong Avatar asked Dec 29 '14 14:12

Karl Wong


Video Answer


1 Answers

Now in Symfony 3.0 they changed the createNamedBuilder, so it's possible to solve this by only calling:

use AppBundle\Form\ShippingTrackCodeReturnType;

$uniqueForm = $this->get('form.factory')->createNamedBuilder('ship_form_'.$orderRecord->getId(), ShippingTrackCodeReturnType::class, $orderRecord)->getForm();

So, you just need to loop to display and save them:

foreach ($invoice->getOrderRecords() as $key => $orderRecord) 
{
    // creates the forms with different names
    $returnShipTrackCodeForm = $this->get('form.factory')->createNamedBuilder('ship_form_'.$orderRecord->getId(), ShippingTrackCodeReturnType::class, $orderRecord)->getForm();

    $returnShipTrackCodeForm->handleRequest($request);
    if ($returnShipTrackCodeForm->isSubmitted() && $returnShipTrackCodeForm->isValid()) 
    {
        // flush object
    }

    $returnForms[$orderRecord->getId()] = $returnShipTrackCodeForm;
}  
like image 131
Pedro Casado Avatar answered Oct 23 '22 04:10

Pedro Casado