E
E
Evgeny Svirsky2014-12-16 16:35:32
JavaScript
Evgeny Svirsky, 2014-12-16 16:35:32

symfony2. How to make independent prototype in a form with a collection, for ajax data loading?

symfony2.
There is a form with a single field of type collection. In it, type is another form. This nested form consists of three (company, country, language) fields, each of which is an entity. All of them are dependent on each other, and loading via ajax. Those. when choosing a company, the necessary countries are loaded, and when choosing a country, the necessary languages ​​are loaded. By default (when adding a new block in the form), the first positions are selected, i.e. the first company, its corresponding country and possible languages.

Everything works except for the validation error case. For example, we chose the first and second companies. They have different countries and languages. In case of an error, the symphony uses the prototype of the collection, but you need to load its own prototype for each element of the collection.

How to make a different prototype for each element of a collection?

HereThis option is not suitable, I say right away.

Actually code:

//SettingsType.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {                            
        $builder->add('domains', 'collection', array(
            'type'=> new ArticleSettingsType($this->user, $this->em, $this->request),
            'options'  => array(
                'required'  => false,
                'attr'      => array('class' => 'email-box')
            ),
            'allow_add'=>true,
            'allow_delete'=>true,
            'by_reference' => false,
            'prototype' => true
        ));  
    }


//ArticleSettingsType.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($this->request->isMethod('POST')) {
            $settingsAdd =$this->request->get('settingsAdd');
            $oc_ids = array();
            $c_ids = array();
            $l_ids = array();
            if (count($settingsAdd['domains'])) {
                foreach($settingsAdd['domains'] as $s) {
                    if (! in_array($s['company'], $oc_ids)) {
                        $oc_ids[] = $s['company'];
                    }
                    if (! in_array($s['country'], $c_ids)) {
                        $c_ids[] = $s['country'];
                    }
                    if (! in_array($s['languages'], $l_ids)) {
                        $l_ids[] = $s['languages'];
                    }
                }
            }
        } else {
            $oCompanyRepo = $this->em->getRepository('EducationBaseBundle:OperationCompany');
            $aUsersCompanies = $oCompanyRepo->getCompaniesOfUser($this->user);

            $oCountryRepo = $this->em->getRepository('EducationBaseBundle:Country');
            $aCompaniesCountries = $oCountryRepo->getAllByUserAndOperationCompany($this->user, $aUsersCompanies[0]);

            $oLanguageRepo = $this->em->getRepository('EducationBaseBundle:Language');
            $aCountriesLanguages = $oLanguageRepo->getAllByUserAndCountry($this->user, key($aCompaniesCountries[0]));

            $oc_ids = array_values($aUsersCompanies);
            $c_ids = array_map(function($a) {return key($a);}, $aCompaniesCountries);
            $l_ids = array_map(function($a) {return key($a);}, $aCountriesLanguages);
        }

        $this->getBuilderCompany($builder, $oc_ids);
        $this->getBuilderCountry($builder, $c_ids);
        $this->getBuilderLanguage($builder, $l_ids);
    }


Maybe there are some thoughts?
Thanks for any info!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Svirsky, 2014-12-16
@e_svirsky

rendered request with form type. added functionality to ajax functions.
prototype in this case will be the same for all elements.

N
neolink, 2014-12-16
@neolink

if you want a normal solution, then carefully read the text on the link that you brought yourself, pass request in form_type frank shit code

B
BoShurik, 2014-12-16
@BoShurik

Drop entity in favor of text + DataTranformer .

$builder
    ->add(
        $builder->create('destination', 'text', array(
            'attr' => array(
                'class' => 'select_station'
            )
        ))
        ->addModelTransformer(new EntityToStringTransformer(
                $options['em'],
                'Acme\DemoBundle\Entity\Station',
                'id'
            )
        )
    )
;

As a replacement for front-end select, I recommend ivaynberg.github.io/select2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question