Symfony 2.8 - Set default value with data from session and propel











up vote
0
down vote

favorite












I use Propel in a project & I try to set default value my form which uses a ModelType input and I need to set a default value stored in session in this form and where this session is not null for the stored value used in this functionnality.



This is my form :



   /**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FclVitrinellisBundleModelProfileVariety',
'name' => 'profile_variety_search',
'locales' => ['fr'],
'session' => null
));
}

/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', ModelType::class, array(
'class' => 'FclVitrinellisBundleModelProfileVariety',
'query' => ProfileVarietyQuery::create()->orderById(),
'property' => 'name',
'label' => 'Profil recherché',
'expanded' => false,
'multiple' => false,
'required' => false,
'placeholder' => '- Filtrer par profil -',
'attr' => array(
'onchange' => 'submit()',
'class' => 'col s3'
)
))
;
}


This is my treatment :



    public function listAction(Request $request = null)
{
$pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
$profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
$session = $request->getSession();
$profileVariety = new ProfileVariety();
$models = null;

$form = $this->createForm(ProfileVarietySearchType::class, $profileVariety);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (null != $form['name']->getData()) {
$models = $pModelManager->getWebsiteByModel($form['name']->getData()->getName());
$session->set('profileVarietySearch', $form['name']->getData()->getName());
} else {
$models = $pModelManager->getList();
}
} else {
if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
$models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
} else {
$models = $pModelManager->getList();
}
}

return $this->render('consolep_model_list.html.twig', array(
'objArray' => $models,
'form' => $form->createView()
));
}


I have try to set default data with 'data' option and with PRE_SET_DATA event in the form but I had satisfactory result.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I use Propel in a project & I try to set default value my form which uses a ModelType input and I need to set a default value stored in session in this form and where this session is not null for the stored value used in this functionnality.



    This is my form :



       /**
    * @param OptionsResolver $resolver
    */
    public function configureOptions(OptionsResolver $resolver)
    {
    $resolver->setDefaults(array(
    'data_class' => 'FclVitrinellisBundleModelProfileVariety',
    'name' => 'profile_variety_search',
    'locales' => ['fr'],
    'session' => null
    ));
    }

    /**
    * @param FormBuilderInterface $builder
    * @param array $options
    */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('name', ModelType::class, array(
    'class' => 'FclVitrinellisBundleModelProfileVariety',
    'query' => ProfileVarietyQuery::create()->orderById(),
    'property' => 'name',
    'label' => 'Profil recherché',
    'expanded' => false,
    'multiple' => false,
    'required' => false,
    'placeholder' => '- Filtrer par profil -',
    'attr' => array(
    'onchange' => 'submit()',
    'class' => 'col s3'
    )
    ))
    ;
    }


    This is my treatment :



        public function listAction(Request $request = null)
    {
    $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
    $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
    $session = $request->getSession();
    $profileVariety = new ProfileVariety();
    $models = null;

    $form = $this->createForm(ProfileVarietySearchType::class, $profileVariety);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    if (null != $form['name']->getData()) {
    $models = $pModelManager->getWebsiteByModel($form['name']->getData()->getName());
    $session->set('profileVarietySearch', $form['name']->getData()->getName());
    } else {
    $models = $pModelManager->getList();
    }
    } else {
    if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
    $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
    } else {
    $models = $pModelManager->getList();
    }
    }

    return $this->render('consolep_model_list.html.twig', array(
    'objArray' => $models,
    'form' => $form->createView()
    ));
    }


    I have try to set default data with 'data' option and with PRE_SET_DATA event in the form but I had satisfactory result.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I use Propel in a project & I try to set default value my form which uses a ModelType input and I need to set a default value stored in session in this form and where this session is not null for the stored value used in this functionnality.



      This is my form :



         /**
      * @param OptionsResolver $resolver
      */
      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults(array(
      'data_class' => 'FclVitrinellisBundleModelProfileVariety',
      'name' => 'profile_variety_search',
      'locales' => ['fr'],
      'session' => null
      ));
      }

      /**
      * @param FormBuilderInterface $builder
      * @param array $options
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('name', ModelType::class, array(
      'class' => 'FclVitrinellisBundleModelProfileVariety',
      'query' => ProfileVarietyQuery::create()->orderById(),
      'property' => 'name',
      'label' => 'Profil recherché',
      'expanded' => false,
      'multiple' => false,
      'required' => false,
      'placeholder' => '- Filtrer par profil -',
      'attr' => array(
      'onchange' => 'submit()',
      'class' => 'col s3'
      )
      ))
      ;
      }


      This is my treatment :



          public function listAction(Request $request = null)
      {
      $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
      $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
      $session = $request->getSession();
      $profileVariety = new ProfileVariety();
      $models = null;

      $form = $this->createForm(ProfileVarietySearchType::class, $profileVariety);
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
      if (null != $form['name']->getData()) {
      $models = $pModelManager->getWebsiteByModel($form['name']->getData()->getName());
      $session->set('profileVarietySearch', $form['name']->getData()->getName());
      } else {
      $models = $pModelManager->getList();
      }
      } else {
      if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
      $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
      } else {
      $models = $pModelManager->getList();
      }
      }

      return $this->render('consolep_model_list.html.twig', array(
      'objArray' => $models,
      'form' => $form->createView()
      ));
      }


      I have try to set default data with 'data' option and with PRE_SET_DATA event in the form but I had satisfactory result.










      share|improve this question















      I use Propel in a project & I try to set default value my form which uses a ModelType input and I need to set a default value stored in session in this form and where this session is not null for the stored value used in this functionnality.



      This is my form :



         /**
      * @param OptionsResolver $resolver
      */
      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults(array(
      'data_class' => 'FclVitrinellisBundleModelProfileVariety',
      'name' => 'profile_variety_search',
      'locales' => ['fr'],
      'session' => null
      ));
      }

      /**
      * @param FormBuilderInterface $builder
      * @param array $options
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('name', ModelType::class, array(
      'class' => 'FclVitrinellisBundleModelProfileVariety',
      'query' => ProfileVarietyQuery::create()->orderById(),
      'property' => 'name',
      'label' => 'Profil recherché',
      'expanded' => false,
      'multiple' => false,
      'required' => false,
      'placeholder' => '- Filtrer par profil -',
      'attr' => array(
      'onchange' => 'submit()',
      'class' => 'col s3'
      )
      ))
      ;
      }


      This is my treatment :



          public function listAction(Request $request = null)
      {
      $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
      $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
      $session = $request->getSession();
      $profileVariety = new ProfileVariety();
      $models = null;

      $form = $this->createForm(ProfileVarietySearchType::class, $profileVariety);
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
      if (null != $form['name']->getData()) {
      $models = $pModelManager->getWebsiteByModel($form['name']->getData()->getName());
      $session->set('profileVarietySearch', $form['name']->getData()->getName());
      } else {
      $models = $pModelManager->getList();
      }
      } else {
      if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
      $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
      } else {
      $models = $pModelManager->getList();
      }
      }

      return $this->render('consolep_model_list.html.twig', array(
      'objArray' => $models,
      'form' => $form->createView()
      ));
      }


      I have try to set default data with 'data' option and with PRE_SET_DATA event in the form but I had satisfactory result.







      forms symfony default-value propel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 17:06

























      asked Nov 21 at 16:51









      NBoulfroy

      386




      386
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          In an EntityType the incoming default 'data' must be an object of the right type. I would first try to see if we have incoming form data.



          public function buildForm(FormBuilderInterface $builder, array $options)
          {
          // Check form data
          $formDataEntity = $builder->getData();

          // Check if it has the field filled in
          if ($formDataEntity && $formDataEntity->getName()) {
          $objToSet = $formDataEntity->getName();
          } else {
          $objToSet = $options['incomingDefaultObject'];
          )

          $builder
          ->add('name', ModelType::class, array(
          class => 'FclVitrinellisBundleModelProfileVariety',
          data => $objToSet,
          ...


          And then for the resolver



          /**
          * @param OptionsResolver $resolver
          */
          public function configureOptions(OptionsResolver $resolver)
          {
          $resolver->setDefaults(array(
          'incomingDefaultObject' => null,
          ));
          }


          And you call the form with the default opion in the controller



          $form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));


          Warning: if a user decides to leave the field empty this code will always set the default.






          share|improve this answer





















          • Thx for your help @Julesezaar, your solution work perfectly ! :O
            – NBoulfroy
            Nov 22 at 9:10




















          up vote
          0
          down vote



          accepted










          I have an other solution.



          Create new Model which his name is ProfileVarietySearch, like this :



          class ProfileVarietySearch
          {
          /** @var null|ProfileVariety $profileVariety */
          private $profileVariety;

          /**
          * @return bool
          */
          public function is_empty()
          {
          return is_null($this->profileVariety);
          }

          /**
          * @return null|ProfileVariety
          */
          public function getProfileVariety()
          {
          return $this->profileVariety;
          }

          /**
          * @param $profileVariety
          *
          * @return ProfileVarietySearch
          */
          public function setProfileVariety($profileVariety): self
          {
          $this->profileVariety = $profileVariety;

          return $this;
          }
          }


          In the controller, write this :



              public function listAction(Request $request = null)
          {
          $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
          $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
          $session = $request->getSession();
          $profileVarietySearch = new ProfileVarietySearch();
          $models = null;

          if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
          $profileVarietySearch->setProfileVariety(
          $profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
          );
          }



          $form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);

          $form->handleRequest($request);

          if ($form->isSubmitted() && $form->isValid()) {
          if (null != $form->getData()->getProfileVariety()) {
          $models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
          $session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
          } else {
          $session->set('profileVarietySearch', null);
          $models = $pModelManager->getList();
          }
          } else {
          if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
          $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
          } else {
          $models = $pModelManager->getList();
          }
          }

          return $this->render($this->view_list, array(
          'objArray' => $models,
          'form' => $form->createView()
          ));
          }


          In the ProfileVarietySearchType, write this :



          public function configureOptions(OptionsResolver $resolver)
          {
          $resolver->setDefaults(array(
          'data_class' => 'FclVitrinellisBundleFormModelProfileVarietySearch',
          'name' => 'profile_variety_search'
          ));
          }

          public function buildForm(FormBuilderInterface $builder, array $options)
          {
          $builder->add('profileVariety', ModelType::class, array(
          'class' => 'FclVitrinellisBundleModelProfileVariety',
          'query' => ProfileVarietyQuery::create()->orderById(),
          'property' => 'name',
          'label' => 'Profil recherché',
          'expanded' => false,
          'multiple' => false,
          'required' => false,
          'placeholder' => '- Filtrer par profil -',
          'attr' => array(
          'onchange' => 'submit()',
          'class' => 'col s3'
          )
          ));
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416942%2fsymfony-2-8-set-default-value-with-data-from-session-and-propel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            In an EntityType the incoming default 'data' must be an object of the right type. I would first try to see if we have incoming form data.



            public function buildForm(FormBuilderInterface $builder, array $options)
            {
            // Check form data
            $formDataEntity = $builder->getData();

            // Check if it has the field filled in
            if ($formDataEntity && $formDataEntity->getName()) {
            $objToSet = $formDataEntity->getName();
            } else {
            $objToSet = $options['incomingDefaultObject'];
            )

            $builder
            ->add('name', ModelType::class, array(
            class => 'FclVitrinellisBundleModelProfileVariety',
            data => $objToSet,
            ...


            And then for the resolver



            /**
            * @param OptionsResolver $resolver
            */
            public function configureOptions(OptionsResolver $resolver)
            {
            $resolver->setDefaults(array(
            'incomingDefaultObject' => null,
            ));
            }


            And you call the form with the default opion in the controller



            $form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));


            Warning: if a user decides to leave the field empty this code will always set the default.






            share|improve this answer





















            • Thx for your help @Julesezaar, your solution work perfectly ! :O
              – NBoulfroy
              Nov 22 at 9:10

















            up vote
            1
            down vote













            In an EntityType the incoming default 'data' must be an object of the right type. I would first try to see if we have incoming form data.



            public function buildForm(FormBuilderInterface $builder, array $options)
            {
            // Check form data
            $formDataEntity = $builder->getData();

            // Check if it has the field filled in
            if ($formDataEntity && $formDataEntity->getName()) {
            $objToSet = $formDataEntity->getName();
            } else {
            $objToSet = $options['incomingDefaultObject'];
            )

            $builder
            ->add('name', ModelType::class, array(
            class => 'FclVitrinellisBundleModelProfileVariety',
            data => $objToSet,
            ...


            And then for the resolver



            /**
            * @param OptionsResolver $resolver
            */
            public function configureOptions(OptionsResolver $resolver)
            {
            $resolver->setDefaults(array(
            'incomingDefaultObject' => null,
            ));
            }


            And you call the form with the default opion in the controller



            $form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));


            Warning: if a user decides to leave the field empty this code will always set the default.






            share|improve this answer





















            • Thx for your help @Julesezaar, your solution work perfectly ! :O
              – NBoulfroy
              Nov 22 at 9:10















            up vote
            1
            down vote










            up vote
            1
            down vote









            In an EntityType the incoming default 'data' must be an object of the right type. I would first try to see if we have incoming form data.



            public function buildForm(FormBuilderInterface $builder, array $options)
            {
            // Check form data
            $formDataEntity = $builder->getData();

            // Check if it has the field filled in
            if ($formDataEntity && $formDataEntity->getName()) {
            $objToSet = $formDataEntity->getName();
            } else {
            $objToSet = $options['incomingDefaultObject'];
            )

            $builder
            ->add('name', ModelType::class, array(
            class => 'FclVitrinellisBundleModelProfileVariety',
            data => $objToSet,
            ...


            And then for the resolver



            /**
            * @param OptionsResolver $resolver
            */
            public function configureOptions(OptionsResolver $resolver)
            {
            $resolver->setDefaults(array(
            'incomingDefaultObject' => null,
            ));
            }


            And you call the form with the default opion in the controller



            $form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));


            Warning: if a user decides to leave the field empty this code will always set the default.






            share|improve this answer












            In an EntityType the incoming default 'data' must be an object of the right type. I would first try to see if we have incoming form data.



            public function buildForm(FormBuilderInterface $builder, array $options)
            {
            // Check form data
            $formDataEntity = $builder->getData();

            // Check if it has the field filled in
            if ($formDataEntity && $formDataEntity->getName()) {
            $objToSet = $formDataEntity->getName();
            } else {
            $objToSet = $options['incomingDefaultObject'];
            )

            $builder
            ->add('name', ModelType::class, array(
            class => 'FclVitrinellisBundleModelProfileVariety',
            data => $objToSet,
            ...


            And then for the resolver



            /**
            * @param OptionsResolver $resolver
            */
            public function configureOptions(OptionsResolver $resolver)
            {
            $resolver->setDefaults(array(
            'incomingDefaultObject' => null,
            ));
            }


            And you call the form with the default opion in the controller



            $form = $this->createForm(YourType::class, $yourObject, array('incomingDefaultObject' => $nameObject));


            Warning: if a user decides to leave the field empty this code will always set the default.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 7:45









            Julesezaar

            40027




            40027












            • Thx for your help @Julesezaar, your solution work perfectly ! :O
              – NBoulfroy
              Nov 22 at 9:10




















            • Thx for your help @Julesezaar, your solution work perfectly ! :O
              – NBoulfroy
              Nov 22 at 9:10


















            Thx for your help @Julesezaar, your solution work perfectly ! :O
            – NBoulfroy
            Nov 22 at 9:10






            Thx for your help @Julesezaar, your solution work perfectly ! :O
            – NBoulfroy
            Nov 22 at 9:10














            up vote
            0
            down vote



            accepted










            I have an other solution.



            Create new Model which his name is ProfileVarietySearch, like this :



            class ProfileVarietySearch
            {
            /** @var null|ProfileVariety $profileVariety */
            private $profileVariety;

            /**
            * @return bool
            */
            public function is_empty()
            {
            return is_null($this->profileVariety);
            }

            /**
            * @return null|ProfileVariety
            */
            public function getProfileVariety()
            {
            return $this->profileVariety;
            }

            /**
            * @param $profileVariety
            *
            * @return ProfileVarietySearch
            */
            public function setProfileVariety($profileVariety): self
            {
            $this->profileVariety = $profileVariety;

            return $this;
            }
            }


            In the controller, write this :



                public function listAction(Request $request = null)
            {
            $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
            $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
            $session = $request->getSession();
            $profileVarietySearch = new ProfileVarietySearch();
            $models = null;

            if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
            $profileVarietySearch->setProfileVariety(
            $profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
            );
            }



            $form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);

            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
            if (null != $form->getData()->getProfileVariety()) {
            $models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
            $session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
            } else {
            $session->set('profileVarietySearch', null);
            $models = $pModelManager->getList();
            }
            } else {
            if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
            $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
            } else {
            $models = $pModelManager->getList();
            }
            }

            return $this->render($this->view_list, array(
            'objArray' => $models,
            'form' => $form->createView()
            ));
            }


            In the ProfileVarietySearchType, write this :



            public function configureOptions(OptionsResolver $resolver)
            {
            $resolver->setDefaults(array(
            'data_class' => 'FclVitrinellisBundleFormModelProfileVarietySearch',
            'name' => 'profile_variety_search'
            ));
            }

            public function buildForm(FormBuilderInterface $builder, array $options)
            {
            $builder->add('profileVariety', ModelType::class, array(
            'class' => 'FclVitrinellisBundleModelProfileVariety',
            'query' => ProfileVarietyQuery::create()->orderById(),
            'property' => 'name',
            'label' => 'Profil recherché',
            'expanded' => false,
            'multiple' => false,
            'required' => false,
            'placeholder' => '- Filtrer par profil -',
            'attr' => array(
            'onchange' => 'submit()',
            'class' => 'col s3'
            )
            ));
            }





            share|improve this answer



























              up vote
              0
              down vote



              accepted










              I have an other solution.



              Create new Model which his name is ProfileVarietySearch, like this :



              class ProfileVarietySearch
              {
              /** @var null|ProfileVariety $profileVariety */
              private $profileVariety;

              /**
              * @return bool
              */
              public function is_empty()
              {
              return is_null($this->profileVariety);
              }

              /**
              * @return null|ProfileVariety
              */
              public function getProfileVariety()
              {
              return $this->profileVariety;
              }

              /**
              * @param $profileVariety
              *
              * @return ProfileVarietySearch
              */
              public function setProfileVariety($profileVariety): self
              {
              $this->profileVariety = $profileVariety;

              return $this;
              }
              }


              In the controller, write this :



                  public function listAction(Request $request = null)
              {
              $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
              $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
              $session = $request->getSession();
              $profileVarietySearch = new ProfileVarietySearch();
              $models = null;

              if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
              $profileVarietySearch->setProfileVariety(
              $profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
              );
              }



              $form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);

              $form->handleRequest($request);

              if ($form->isSubmitted() && $form->isValid()) {
              if (null != $form->getData()->getProfileVariety()) {
              $models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
              $session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
              } else {
              $session->set('profileVarietySearch', null);
              $models = $pModelManager->getList();
              }
              } else {
              if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
              $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
              } else {
              $models = $pModelManager->getList();
              }
              }

              return $this->render($this->view_list, array(
              'objArray' => $models,
              'form' => $form->createView()
              ));
              }


              In the ProfileVarietySearchType, write this :



              public function configureOptions(OptionsResolver $resolver)
              {
              $resolver->setDefaults(array(
              'data_class' => 'FclVitrinellisBundleFormModelProfileVarietySearch',
              'name' => 'profile_variety_search'
              ));
              }

              public function buildForm(FormBuilderInterface $builder, array $options)
              {
              $builder->add('profileVariety', ModelType::class, array(
              'class' => 'FclVitrinellisBundleModelProfileVariety',
              'query' => ProfileVarietyQuery::create()->orderById(),
              'property' => 'name',
              'label' => 'Profil recherché',
              'expanded' => false,
              'multiple' => false,
              'required' => false,
              'placeholder' => '- Filtrer par profil -',
              'attr' => array(
              'onchange' => 'submit()',
              'class' => 'col s3'
              )
              ));
              }





              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                I have an other solution.



                Create new Model which his name is ProfileVarietySearch, like this :



                class ProfileVarietySearch
                {
                /** @var null|ProfileVariety $profileVariety */
                private $profileVariety;

                /**
                * @return bool
                */
                public function is_empty()
                {
                return is_null($this->profileVariety);
                }

                /**
                * @return null|ProfileVariety
                */
                public function getProfileVariety()
                {
                return $this->profileVariety;
                }

                /**
                * @param $profileVariety
                *
                * @return ProfileVarietySearch
                */
                public function setProfileVariety($profileVariety): self
                {
                $this->profileVariety = $profileVariety;

                return $this;
                }
                }


                In the controller, write this :



                    public function listAction(Request $request = null)
                {
                $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
                $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
                $session = $request->getSession();
                $profileVarietySearch = new ProfileVarietySearch();
                $models = null;

                if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
                $profileVarietySearch->setProfileVariety(
                $profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
                );
                }



                $form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);

                $form->handleRequest($request);

                if ($form->isSubmitted() && $form->isValid()) {
                if (null != $form->getData()->getProfileVariety()) {
                $models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
                $session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
                } else {
                $session->set('profileVarietySearch', null);
                $models = $pModelManager->getList();
                }
                } else {
                if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
                $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
                } else {
                $models = $pModelManager->getList();
                }
                }

                return $this->render($this->view_list, array(
                'objArray' => $models,
                'form' => $form->createView()
                ));
                }


                In the ProfileVarietySearchType, write this :



                public function configureOptions(OptionsResolver $resolver)
                {
                $resolver->setDefaults(array(
                'data_class' => 'FclVitrinellisBundleFormModelProfileVarietySearch',
                'name' => 'profile_variety_search'
                ));
                }

                public function buildForm(FormBuilderInterface $builder, array $options)
                {
                $builder->add('profileVariety', ModelType::class, array(
                'class' => 'FclVitrinellisBundleModelProfileVariety',
                'query' => ProfileVarietyQuery::create()->orderById(),
                'property' => 'name',
                'label' => 'Profil recherché',
                'expanded' => false,
                'multiple' => false,
                'required' => false,
                'placeholder' => '- Filtrer par profil -',
                'attr' => array(
                'onchange' => 'submit()',
                'class' => 'col s3'
                )
                ));
                }





                share|improve this answer














                I have an other solution.



                Create new Model which his name is ProfileVarietySearch, like this :



                class ProfileVarietySearch
                {
                /** @var null|ProfileVariety $profileVariety */
                private $profileVariety;

                /**
                * @return bool
                */
                public function is_empty()
                {
                return is_null($this->profileVariety);
                }

                /**
                * @return null|ProfileVariety
                */
                public function getProfileVariety()
                {
                return $this->profileVariety;
                }

                /**
                * @param $profileVariety
                *
                * @return ProfileVarietySearch
                */
                public function setProfileVariety($profileVariety): self
                {
                $this->profileVariety = $profileVariety;

                return $this;
                }
                }


                In the controller, write this :



                    public function listAction(Request $request = null)
                {
                $pModelManager = $this->get('fcl_vitrinellis.p_model_manager');
                $profileVarietyManager = $this->get('fcl_vitrinellis.profile_variety_manager');
                $session = $request->getSession();
                $profileVarietySearch = new ProfileVarietySearch();
                $models = null;

                if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
                $profileVarietySearch->setProfileVariety(
                $profileVarietyManager->getByName($session->get('profileVarietySearch'))->getData()[0]
                );
                }



                $form = $this->createForm(ProfileVarietySearchType::class, $profileVarietySearch);

                $form->handleRequest($request);

                if ($form->isSubmitted() && $form->isValid()) {
                if (null != $form->getData()->getProfileVariety()) {
                $models = $pModelManager->getWebsiteByModel($form->getData()->getProfileVariety()->getName());
                $session->set('profileVarietySearch', $form->getData()->getProfileVariety()->getName());
                } else {
                $session->set('profileVarietySearch', null);
                $models = $pModelManager->getList();
                }
                } else {
                if ($session->has('profileVarietySearch') && null != $session->get('profileVarietySearch')) {
                $models = $pModelManager->getWebsiteByModel($session->get('profileVarietySearch'));
                } else {
                $models = $pModelManager->getList();
                }
                }

                return $this->render($this->view_list, array(
                'objArray' => $models,
                'form' => $form->createView()
                ));
                }


                In the ProfileVarietySearchType, write this :



                public function configureOptions(OptionsResolver $resolver)
                {
                $resolver->setDefaults(array(
                'data_class' => 'FclVitrinellisBundleFormModelProfileVarietySearch',
                'name' => 'profile_variety_search'
                ));
                }

                public function buildForm(FormBuilderInterface $builder, array $options)
                {
                $builder->add('profileVariety', ModelType::class, array(
                'class' => 'FclVitrinellisBundleModelProfileVariety',
                'query' => ProfileVarietyQuery::create()->orderById(),
                'property' => 'name',
                'label' => 'Profil recherché',
                'expanded' => false,
                'multiple' => false,
                'required' => false,
                'placeholder' => '- Filtrer par profil -',
                'attr' => array(
                'onchange' => 'submit()',
                'class' => 'col s3'
                )
                ));
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered Nov 23 at 14:40









                NBoulfroy

                386




                386






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416942%2fsymfony-2-8-set-default-value-with-data-from-session-and-propel%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks