Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Form;
- use Symfony\Component\Form\AbstractType;
- use Symfony\Component\Form\CallbackTransformer;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use App\Entity\Information;
- use App\Entity\Traveler;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\FileType;
- use App\Entity\PossibleAnswer;
- use Doctrine\ORM\EntityRepository;
- use Symfony\Component\Validator\Constraints\File;
- use phpDocumentor\Reflection\Types\Null_;
- use Symfony\Component\Validator\Constraints\Length;
- class TravelerInformationType extends AbstractType
- {
- private $entityManager;
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->entityManager = $entityManager;
- }
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $possibleanswerCallBackTransformer = new CallbackTransformer(
- function ($possibleanswer_id) {
- if (!empty($possibleanswer_id)) {
- $possibleanswer = $this->entityManager
- ->getRepository(PossibleAnswer::class)
- ->find($possibleanswer_id)
- ;
- return $possibleanswer;
- } else {
- return null;
- }
- }, function (PossibleAnswer $possibleanswer = null) {
- if ($possibleanswer !== null) {
- return $possibleanswer->getId();
- } else {
- return null;
- }
- }
- );
- // si une info a déjà été renseignée pour le voyageur, je dois renseigner le champ avec la valeur
- $data = $builder->getData();
- // je récupère la liste des questions active pour ce pays
- $informations = $options['informations'];
- $upload_dir = $options['upload_dir'];
- /* @var Information $information */
- foreach ($informations as $information) {
- $field_options = array();
- $type = null;
- $field_options['data'] = $data[$information->getId()] ?? null;
- switch ($information->getType()) {
- case 'str':
- $type = TextType::class;
- if (!empty($information->getLength())) {
- $field_options['constraints'] = new Length(array(
- 'max' => $information->getLength()
- ));
- }
- break;
- case 'bool':
- $type = CheckboxType::class;
- $field_options['data'] = boolval($field_options['data']);
- break;
- case 'file':
- $type = FileType::class;
- $field_options['constraints'] = new File(array(
- 'maxSize' => '5M',
- 'maxSizeMessage' => 'Le fichier ne peut dépasser 5Mo',
- 'mimeTypes' => ['application/pdf', 'application/x-pdf', 'image/jpg', 'image/jpeg', 'image/png'],
- 'mimeTypesMessage' => 'Le fichier doit être au format .pdf, .jpg ou .png',
- ));
- $filepath = $upload_dir.'/'.$field_options['data'];
- if (file_exists($filepath) && is_file($filepath)) {
- $file = new \Symfony\Component\HttpFoundation\File\File($filepath);
- $field_options['data'] = ($field_options['data'] !== null && $file->isFile()) ? $file : null;
- }
- else {
- $field_options['data'] = null;
- }
- $field_options['empty_data'] = $field_options['data'];
- break;
- case 'list-radio':
- $field_options['expanded'] = true;
- $field_options['placeholder'] = false;
- case 'list-choice':
- $type = EntityType::class;
- $field_options['class'] = PossibleAnswer::class;
- $field_options['choice_label'] = 'value';
- $field_options['query_builder'] = function (EntityRepository $er) use ($information) {
- return $er->getActiveAnswerForInformationQuery($information);
- };
- break;
- }
- $field_options['required'] = false;
- $field_options['label'] = $information->getName();
- if ($information->getCategory() !== null) {
- $field_options['attr']['data-title'] = $information->getCategory()->getName();
- }
- if (!empty($information->getHelp())) {
- $field_options['help'] = $information->getHelp();
- }
- $builder->add($information->getId(), $type, $field_options);
- // je rajoute le data transformer pour les listes
- if (substr($information->getType(), 0, 5 ) === 'list-') {
- $builder
- ->get($information->getId())
- ->addModelTransformer($possibleanswerCallBackTransformer)
- ;
- }
- if (!empty($options['traveler_display'])) {
- $builder
- ->add('traveler', TravelerCollectionType::class, array(
- 'data' => new Traveler(),
- 'mapped' => false,
- 'label' => false,
- ))
- ;
- }
- }
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults(array(
- // 'data_class' => User::class, // je ne travaille pas avec une entité réelle pour ce form
- 'informations' => null,
- 'upload_dir' => null,
- 'traveler' => null,
- 'traveler_hide' => null,
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement