V
V
Vladimir Leitenberger2015-03-31 10:27:49
symfony
Vladimir Leitenberger, 2015-03-31 10:27:49

symfony2. How to make an elementary filter for a form?

It is necessary to make a more or less primitive filter. There is a table with data, which would not hurt to filter a bit by the selected (by default - the current year) year in a simple drop-down.
The only conditions are:
- the current year is always in the list
- the other years (eg 2013, 2014) are read (grouped and aggregated) from the database
- regular POST/GET , no AJAX yet
- using symfony2-forms is preferable.
UPDATE:
just settled on this one so far:

namespace MyBundle\Model\Filter;

class YearModel
{

    /**
     * @var array
     */
    private $years = array();

    /**
     * @return array
     */
    public function getYears()
    {
        return $this->years;
    }

    /**
     * @param $year
     * @return YearModel
     */
    public function addYear( $year )
    {
        /** .... добавить год в список, если еще нет ... */
    }

    /**
     * @param $year
     * @return YearModel
     */
    public function removeYear( $year )
    {
        /** .... удалить, если есть ... */
    }
}

and the form class:
namespace MyBundle\Form\Filter;

use /** use statements */

class YearFilterType extends AbstractType
{
    const FILTER_TYPE_NAME = 'years_formfilter';

    public function buildForm( FormBuilderInterface $builder, array $options )
    {
      cb_pre_set_data = function( FormEvent $o_event ) use ($em) {
         // достучаться до модели
         // прочитать данные из базы
         // обновить модель 
      };
     
      $builder->add('years', 'choice', array(
        'required'      => true,
        'placeholder'   => false,
      ));
     $builder->addEventListener(FormEvents::PRE_SET_DATA, $cb_pre_set_data);
    }

    public function setDefaultOptions( OptionsResolverInterface $resolver )
    {
       $resolver->setDefaults(array(
          'csrf_protection' => false,
          'data_class'      => "MyBundle\\Model\\Filter\\YearModel"
       ));
    }

    public function getName()
    {
       return self::FILTER_TYPE_NAME;
    }
}

but doesn't work. more precisely, the list is ALWAYS empty ... I can’t understand what I stumble on

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2015-03-31
@lexxpavlov

I'm using the Datatables jQuery plugin . He knows a lot, including help in this task.
Personally, I inserted directly through the templates, but now I saw that there are two bundles: stwe/DatatablesBundle and brown298/data-tables-bundle .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question