lexik / LexikFormFilterBundle

This Symfony bundle aim to provide classes to build some form filters and then build a doctrine query from this form filter.
MIT License
389 stars 119 forks source link

CollectionAdapterFilterType display an unwanted "level" with a label : "0" #325

Closed jorismulliez closed 4 years ago

jorismulliez commented 4 years ago

Hello,

I'm using this bundle for filtering a list of entity (subscription). All works well, except for a special case with CollectionAdapterFilterType. I have invoice entity related to subscriptions and I want to be able to filter by fields under the invoice entity. I followed this documentation : https://github.com/lexik/LexikFormFilterBundle/blob/master/Resources/doc/working-with-the-bundle.md#iii-working-with-entity-associations-and-embeddeding-filters And it is ok I can filter as I want.

But... the form render an unwanted "0" label between my "Invoices" (Factures in french) and the fields of the invoice entity.

An image will be more explicit : Capture du 2019-12-11 15:28:50

I don't know if the problem come from my form configuration, from my templates/theme or from my use of this bundle. If you could guide me on the path to explore....

Here are some extract of the code : AdminTrainingformListType.php:

// [...]
class AdminTrainingformListType extends AbstractType {
// [...]

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(/* ... */)
        // [...]
       ->add('invoices', Filters\CollectionAdapterFilterType::class, array(
                'label' => 'invoices.label',
                'entry_type' => new InvoicesFilterType(),
                'required' => false,
                'add_shared' => function (FilterBuilderExecuterInterface $qbe) {
                    $closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) {
                        // add the join clause to the doctrine query builder
                        // the where clause for the label and color fields will be added automatically with
                        // the right alias later by the Lexik\Filter\QueryBuilderUpdater
                        $filterBuilder->leftJoin($alias.'.invoices', $joinAlias);
                    };

                    // then use the query builder executor to define the join and its alias.
                    $qbe->addOnce($qbe->getAlias().'.invoices', 'inv', $closure);
                },
            ))
      // [...]
      ;
    }

    public function getBlockPrefix()
    {
        return 'Ftf';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'validation_groups' => array('filtering'), // avoid NotBlank() constraint-related message
        ));
    }
}

InvoicesFilterType.php:

// [...]
class InvoicesFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('paymentMethod', Filters\ChoiceFilterType::class, [
            'label' => 'paymentMethod.label',
            'choices' => [
                'paymentMethod.chq' => 'chq',
                'paymentMethod.transfer' => 'transfer',
                'paymentMethod.cash' => 'cash',
                'paymentMethod.cb' => 'cb',
            ],
            'choices_as_values' => true,
            'multiple' => true,
            'expanded' => true,
        ])
        ->add('number', Filters\TextFilterType::class, [
            'label' => 'number',
            'help' => 'numberHelp',
        ])
        ->add('unpaid', /* ... */);
    }

    public function getBlockPrefix()
    {
        return 'Ftf_invoices';
    }
}

I'm available for any informations I can give you to better understand the problem!

Thanks for your help,

snebes commented 4 years ago

This is actually a Symfony/Form thing. You will want to set the label on the InvoicesFilterType to false, by adding a configureOptions method:

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'label' => false,
        ]);
    }
jorismulliez commented 4 years ago

Thanks a lot for your quick response !

It's all right now !

And thanks for this bundle :)