I work on a Symfony 2.8 project. The system work with TextType / DateTimeType / MoneyType but not for EntityType & DateType also.
I try to define default value on my EntityType from a value in session which is passed in parameters when I create the form in Controller like this:
$filter_form = $this->createForm(FilterActeType::class, null, [
'filters' => array_merge(
$defaultFilter,
$paginatorService->getFiltersFromSessionByContext($usr->getId(), $request->attributes->get('_route'))
)
]);
I have try to use data
and choice_value
parameters in the EntityType but the problem is the same : when I reload the page, the data is not present in the form filter while the data is in variable session.
Case with data parameter without anonymous function:
$builder->add('etude', EntityType::class, [
'label' => $this->translator->trans('Étude'),
'class' => Etude::class,
'required' => false,
'attr' => array('dyn-form-data' => 'cabinet,createur,destinataire'), // Champs dynamique => on renseigne le ou les enfants (le(s) champ(s) qui doit être mise à jour à la modification de celui-ci)
'data' => $this->getDataFromFilters($options, 'etude'),
]);
Observations: this case has been returned an error message : "... passed to the choice field must be managed. Maybe you forget to persist it in the entity manager ?".
Case with data parameter with anonymous function:
$builder->add('etude', EntityType::class, [
'label' => $this->translator->trans('Étude'),
'class' => Etude::class,
'required' => false,
'attr' => array('dyn-form-data' => 'cabinet,createur,destinataire'), // Champs dynamique => on renseigne le ou les enfants (le(s) champ(s) qui doit être mise à jour à la modification de celui-ci)
'data' => function (EntityRepository $er) use ($options) {
if (null != $etude = $this->getDataFromFilters($options, 'etude')) {
$er->createQueryBuilder('e')->andWhere('e.id = :id')->setParameter('id', $etude->getId());
}
}
]);
Observations:: no error returns but no data also.
Case with choice_value parameter:
$builder->add('etude', EntityType::class, [
'label' => $this->translator->trans('Étude'),
'class' => Etude::class,
'required' => false,
'attr' => array('dyn-form-data' => 'cabinet,createur,destinataire'), // Champs dynamique => on renseigne le ou les enfants (le(s) champ(s) qui doit être mise à jour à la modification de celui-ci)
'choice_value' => function() use ($options) {
return $this->getDataFromFilters($options, 'etude');
},
]);
Observations: no error returns and no data also.
Code from getDataFromFilters method in Form object:
/**
* @param array $filters
* @param string $field
*
* @return \DateTime|Etude|Cabinet|string|int|float|TypeDocument|ModeDistribution|null
*/
private function getDataFromFilters(array $options, string $field)
{
return (isset($options['filters'][$field]) && (null != $value = $options['filters'][$field])) ? $value : null;
}
EDIT (24/11/2021): debug added
This is the debug of the entity stored in session:
etude: Etude {#812 ▼
+__isInitialized__: true
-id: 1
-libelle: "Toto"
-mail: "yolo.toto@toto-france.fr"
-origines: PersistentCollection {#811 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#810 ▼
-elements: []
}
#initialized: false
}
-utilisateurs: PersistentCollection {#809 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#808 ▼
-elements: []
}
#initialized: false
}
-cabinets: PersistentCollection {#807 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#806 ▼
-elements: array:1 [▼
0 => Cabinet {#805 ▼
-id: 1
-libelle: "Cabinet Toto"
-etude: Etude {#812 …2}
-infoUserByEtude: PersistentCollection {#804 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#803 ▼
-elements: []
}
#initialized: false
}
-adresse: null
-villeCP: null
}
]
}
#initialized: true
}
-actes: PersistentCollection {#802 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#801 ▼
-elements: []
}
#initialized: false
}
-typeDocuments: PersistentCollection {#800 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#799 ▼
-elements: []
}
#initialized: false
}
-modeDistributions: PersistentCollection {#798 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#797 ▼
-elements: []
}
#initialized: false
}
-themeMainColor: null
-themeAccentColor: null
-logo: null
-background: null
-sous_domaine: "yolo"
-transactions: PersistentCollection {#794 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#795 ▼
-elements: []
}
#initialized: false
}
-templates: PersistentCollection {#796 ▼
-snapshot: []
-owner: null
-association: null
-em: null
-backRefFieldName: null
-typeClass: null
-isDirty: false
#collection: ArrayCollection {#138 ▼
-elements: []
}
#initialized: false
}
-aliasMail: "contact@toto-france.fr"
-smsActif: false
-notificationSmsActif: false
-paiementActif: true
-blocNoteActif: false
-connexionDirecteActif: false
-dureeTokenConnexionDirecte: 2
-smsSenderName: null
-locale: null
-faxActif: false
-dossierDebiteurActif: true
-defaultModuleActif: true
-frequenceNotification: "day"
-objetMail: null
-lienRgpd1: null
-lienRgpd2: null
-mailEchecNotification: null
…2
}