2016-05-21 4 views
1

Ich versuche, die Symfony2 dynamische Form Modifikation Tutorial http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-dataSymfony2 Forms - Wie wird das Feld eines verbundenen Elements automatisch ausgefüllt?

Mein Schema ist ein bisschen anders als die eine, die sie in ihrem Tutorial zu folgen. Mine besteht aus den folgenden Beziehungen:

Land (one to many) Büro
Amt (one to many) Mitarbeiter

Wenn ich einen vorhandenen Mitarbeiter bearbeiten, würde ich es wie das Land zu laden, wenn das Amt ist Als Standardoption angezeigt, zusätzlich zu den Niederlassungen in diesem Land nur im Dropdown-Menü "Office" (sofern kein anderes Land ausgewählt wurde, sollte der jQuery-Code (nicht enthalten) dies entsprechend ändern).

Das Ergebnis jedoch; ist, dass das Feld Land immer noch den Platzhalterwert anstelle des korrekten Landes für das Büro des Mitarbeiters anzeigt. (Auf der Plusseite zeigt das Office-Dropdown nur die Büros für dieses Land an, was bedeutet, dass der Aufruf $ country-> getOffices() funktioniert, so dass ich mit dem richtigen Country-Objekt arbeite. Ich kann es einfach nicht auswählen lassen standardmäßig).

Befolge ich hier die Best Practice? Gibt es etwas, das mir fehlt und das mir keine Werte in Form für verwandte Entitäten zuweist?

Code:

class EmployeeType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
     ->add('name') 
     ->add('country', EntityType::class, array(
      'class' => 'AppBundle:Country', 
      'mapped' => false, 
      'placeholder' => '=== Select a Country ===', 
     )) 
    ; 

    $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function (FormEvent $event) { 
      $form = $event->getForm(); 

      // This will be the Employee entity 
      $data = $event->getData(); 

      $office = $data->getOffice(); 
      $country = null === $office ? array() : $office->getCountry(); 

      $form->get('country')->setData($country); // I think this is what's not working properly. 

      $form->add('office', EntityType::class, array(
       'class'  => 'AppBundle:Office', 
       'placeholder' => '=== Select an Office ===', 
       'choices'  => $country->getOffices(), 
     )); 
     } 
    ); 
} 

Antwort

1

hatte ich eine Chance, schnell das Tutorial Link Referenz zu lesen, und ich denke, du hast recht, wo der Fehler auftritt.

Ich denke (aber ich bin mir nicht sicher), dass dies das Update:

$office = $data->getOffice(); 
$offices = null === $office ? array() : $office->getCountry()->getOffices(); 

$form->add('office', EntityType::class, array(
    'class'  => 'AppBundle:Office', 
    'placeholder' => '=== Select an Office ===', 
    'choices'  => $offices, 
)); 

zeige ich nur die entsprechenden Abschnitte, die Sie ändern müssen. Probieren Sie es aus und sehen Sie, ob das hilft.

+0

Das Problem ist, dass ich nicht eine Beziehung einer Beziehung bevölkern können. z.B. Das Land des Mitarbeiters, da Land eine Beziehung zum Office-Objekt hat und das Office-Objekt eine Beziehung zum Objekt Mitarbeiter hat. Mit meinem obigen Code kann ich das Land nicht bevölkern und bin mir nicht sicher warum. – Saintwolf

0

Versuchen Sie, die Daten über die Veranstaltung statt zu ändern:

Ich habe
class EmployeeType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name') 
      ->add('country', EntityType::class, array(
       'class' => 'AppBundle:Country', 
       'mapped' => false, 
       'placeholder' => '=== Select a Country ===', 
      )) 
     ; 

     $builder->addEventListener(
      FormEvents::PRE_SET_DATA, 
      function (FormEvent $event) { 
       $form = $event->getForm(); 

       // This will be the Employee entity 
       $data = $event->getData(); 

       $office = $data->getOffice(); 

       // since the country field is not set as "multiple" 
       // the data should not be an array but a string 
       $country = null === $office ? '' : $office->getCountry(); 

       $data['country'] = $country(->getName()?); 
       $event->setData($data); // may work 

       $form->add('office', EntityType::class, array(
        'class'  => 'AppBundle:Office', 
        'placeholder' => '=== Select an Office ===', 
        'choices'  => $country->getOffices(), 
       )); 
      } 
     ); 
    } 
}