LexikFormFilterBundle icon indicating copy to clipboard operation
LexikFormFilterBundle copied to clipboard

Question : Extend Base FormFilterType

Open Farshadi73 opened this issue 7 years ago • 2 comments

Hi, This is my first time use this Bundle and English not my native language so I'm sorry for my mistakes.

screenshot from 2018-03-28 14-14-37

I create my own FilterType and extend Basic filter type of this bundle ... When I use my filtertype in FormFilterType, form type not work and querybuilder not change ... But when use basic formfiltertype instead of my filtertype everything is fine ... Where is my mistake?

Update: Why I need to override types?

I have several entity and all of them has Id , TrackingNumber ... I need to define label or other options in one place and use that in all of entityFilters ...

class RequestFilterType extends AbstractType
{
    /**
     * {@inheritdoc}
     *
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('trackingNumber', TrackingNumberFilterType::class)
    }

    /**
     * {@inheritdoc}
     *
     * @return null|string
     */
    public function getParent()
    {
        return FormFilterType::class;
    }

    /**
     * {@inheritdoc}
     *
     * @return null|string
     */
    public function getBlockPrefix()
    {
        return 'assessment_request_filter';
    }
}
___________________________

class TrackingNumberFilterType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('trackingNumber', NumberFilterType::class, array(
                'label' => false,
                'attr'  => array(
                    'placeholder' => 'narmafzam.fields.trackingNumber'
                )
            ));
    }

    /**
     * {@inheritdoc}
     *
     * @return null|string
     */
    public function getBlockPrefix()
    {
        return 'narmafzam_tracking_number_filter';
    }
}

Farshadi73 avatar Mar 28 '18 10:03 Farshadi73

Perhaps something like the following would work for you, since it looks like you just want to populate some default values to some existing options.

// src/AppBundle/Form/TrackingNumberFilterType.php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\NumberFilterType;

class TrackingNumberFilterType extends AbstractType
{
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'label'       => false,
            'placeholder' => 'narmafzam.fields.trackingNumber',
        ]);
    }

    /**
     * @return string
     */
    public function getParent()
    {
        return NumberFilterType::class;
    }
}
// src/AppBundle/Form/RequestFilterType.php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RequestFilterType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('trackingNumber', TrackingNumberFilterType::class)
    }
}

antoniocambados avatar Apr 02 '18 16:04 antoniocambados

Up?

gilles-g avatar May 23 '18 06:05 gilles-g