2
votes

Maybe i have a very dumb question, but i'm new in Symfony2 and i was wondering if i can build a Search Form with Symfony Form Component, the same way I do with a Registration Form for example.

My Search form will have a Country select field, a Club select field, a Gender radio buttons field a Level select field and the submit button.

Is it possible to do that with the Form Component or to do something like this is better to just build the search form directly in the view?

I've been searching for information about this, but I didn't find anything.

Here is how my SearchPlayerType.php looks like.

<?php 
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityRepository;

class SearchPlayersType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('country', 'entity', array(
            'placeholder' => 'Choose a country',
            'class' => 'AppBundle:Country',
            'property' => 'name',
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('c')->orderBy('c.name', 'ASC');
            },
            ))
        ->add('club', 'entity', array(
            'placeholder' => 'Choose a club',
            'class' => 'AppBundle:Club',
            'property' => 'name',
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('c')->orderBy('c.name', 'ASC');
            },
            ))
        ->add('gender', 'entity', array(
            'class' => 'AppBundle:Gender',
            'property' => 'name',
            'expanded' => true
            ))
        ->add('level', 'rating', array(
            'label' => 'Playing Level',
            'stars' => 5))
    ;

}

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

If it is possible to do it this way I don´t know what Entity my data_class needs to be

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Usuario'
    ));
}
}
?>
1
What you have try so far. Please post the code that you have currently ?Sulthan Allaudeen
I edited my post and you can see what I have untill now. I'm doubting if this is the correct way to do something like this. @SulthanAllaudeen.nestor
It seems you're building correctly, i am not sure if require and do filters within itselfSulthan Allaudeen

1 Answers

1
votes

You can go two ways with this: you can either create a form specific model for your search form which will either be a really thin object with public properties; or you can remove the entry for data_class in your form options which will switch your form into returning an array rather than an object (documentation).

The former is the more OO way of doing things and allows you to add validation annotations without embedding those within the form, this way also means you can add getters and setters that transform your search data very easily and not cluttering up your controllers. So your model would look something like:

namespace MyBundle\Form\Model;

class SearchModel
{
    public $country;
    public $club;
    // ...
}

The alternate is just:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        // don't set 'data_class' in here
    ));
}

Then when you do $form->getData() you'll just get an array back rather than an object.