miltone
miltone

Reputation: 4721

set form data with session value without submit this symfony form

Hi everybody symfonistes,

I would like to use symfony's form in the web page for search data and when those data are validated I use a table with pagination system for viewing that in the same webpage where the form is.

My webpage runs correctly with search engine and pagination system table but just one thing not run. When I click a button for system pagination (sort, page number or number of viewing per page) a request is send to the controller and at this moment I LOST MY DATA CHOICE FORM.

The result of system pagination is good and relative to previous search but the data form disappear. That way the user can use my webpage but he doesn't look which parameters for the result.

I would like to use session for rebuild my form with data defined previously. at this moment I use preferred_choices in my form class (not run) but if you have another solving you welcome.

Or can you tell me why my form preferred_choices not run ?

Thank a lot for your support

my form class :

class CqsProSansMarqueType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $defaultCQSsearch = new CqsProSansMarque();
    ladybug_dump($options['attr']);
    $defaultCQSsearch->setRayLibelle((!array_key_exists('ray_libelle',     $options['attr'])) ? null : $options['attr']['ray_libelle']);
    $defaultCQSsearch->setFamLibelle((!array_key_exists('fam_libelle', $options['attr'])) ? null : $options['attr']['fam_libelle']);
    $defaultCQSsearch->setCaeLibelle((!array_key_exists('cae_libelle', $options['attr'])) ? null : $options['attr']['cae_libelle']);

    ladybug_dump($defaultCQSsearch);

    $builder
        ->add('ray_libelle', 'entity', array(
            'class' => 'ApplicationDriveBundle:CqsProSansMarque',
            'property' => 'ray_libelle',
            'query_builder' => function(CqsProSansMarqueRepository $er){
                return $er->createQueryBuilder('a')
                ->select('a')
                ->groupBy('a.ray_libelle');
            },
            'preferred_choices' => array($defaultCQSsearch),
            'label' => 'rayon',
            'required' => false,
        ))
        ->add('fam_libelle', 'entity', array(
            'class' => 'ApplicationDriveBundle:CqsProSansMarque',
            'property' => 'fam_libelle',
            'query_builder' => function(CqsProSansMarqueRepository $er){
                    return $er->createQueryBuilder('a')
                        ->select('a')
                        ->groupBy('a.fam_libelle');
                },
            'label' => 'famille',
            'required' => false,
        ))

    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Application\DriveBundle\Entity\CqsProSansMarque',
    ));
}

public function getName()
{
    return 'CqsProSansMarque';
}

}

my controller construct's form :

            $searchForm2 = $this->createForm(new CqsProSansMarqueType(), $CqsProSansMarque, array(
            'action' => $this->generateUrl('QualityPage_proSansMarque_search', array('maxItemsPerPage' => $maxItemsPerPage)),
            'method' => 'POST',
            'attr'   =>  array('ray_libelle' => $CqsProSansMarque->getRayLibelle()),
        ));

I tested my $defaultCQSsearch variable. It seems to be good setted.

Upvotes: 0

Views: 2972

Answers (1)

Marino Di Clemente
Marino Di Clemente

Reputation: 3190

there are more than one choice to do what you want:

1) submitting data stored in session

you can submit data to a form in two ways:

  • using handleRequest, in this case you pass the entire request
  • using submit, in this case you pass directly the fields to the form

so to keep your form fields under a session you should check if form is submitted, and if not, submit the data (cloned from request) previously stored in the session, this can be done over the form class, maybe in your controller. here is a link that explain how to use submit method.

2) using event Form::PRE_SET_DATA

you can define an eventlistener or a subscriber that modify the data(maybe an entity) of the form based on session. in this case you need to inject the session(and then the request) in the form class or in the eventsubscriber this choice is more decoupled compared to the first(and can be reused with an eventsubscriber) but more hard. here how to use eventlistener or eventsubscriber to make a form dynamic

3) storing the submitted form data class in session

and using it to create the new form (as second parameter of createForm)

The approach most widely used is the third but there are problems related to the deserialization of the object in the session (in your case the related entities will be detached from doctrine), for this reason i suggest you to try the first or second choice which are still valid.

Upvotes: 1

Related Questions