Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Question : Extend Base FormFilterType #289

Open
Farshadi73 opened this issue Mar 28, 2018 · 2 comments
Open

Question : Extend Base FormFilterType #289

Farshadi73 opened this issue Mar 28, 2018 · 2 comments

Comments

@Farshadi73
Copy link

Farshadi73 commented Mar 28, 2018

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';
    }
}
@antoniocambados
Copy link

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)
    }
}

@gilles-g
Copy link
Member

Up?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants