Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

php - 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' => 'MainAdminBundleEntityLanguage',
                   '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');

}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

use AppBundleFormShippingTrackCodeReturnType;

$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;
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...