rdohms / dms-filter-bundle

Provides a FilterService for Symfony to allow users to implement input filtering in entities using Annotations
MIT License
77 stars 24 forks source link

[2.0.0 Beta 2] Doesn't filter before validation #18

Closed ruudk closed 10 years ago

ruudk commented 10 years ago

I'm using the latest beta but the filtering before validation doesn't seem to work.

<?php

namespace AppBundle\Form;

use AppBundle\Form\CustomType;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Iban;

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('bank_iban', 'text', array(
            'label' => 'user.profile.label.bank_iban',
            'constraints'               => array(
                new Iban(array(
                    'message' => 'user.account_number.invalid_iban',
                )),
            )
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\User',
        ));
    }

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

And my user entity contains this:

/**
 * @var string $bank_iban
 *
 * @Filter\ToUpper
 * @ORM\Column(name="bank_iban", type="string", nullable=true)
 */
private $bank_iban;

When I enter a valid IBAN in lowercase, it complains that the IBAN is not correct. So it doesn't filter before validation. It validates and then applies the filters.

ruudk commented 10 years ago

When I add the constraints as annotations to my entity, it works as expected:

/**
 * @var string $bank_iban
 *
 * @Filter\ToUpper
 * @Assert\Iban
 * @ORM\Column(name="bank_iban", type="string", nullable=true)
 */
private $bank_iban;
rdohms commented 10 years ago

@ruudk interesting i'm going to investigate why Symfony switches that order.

rdohms commented 10 years ago

@ruudk ok, I managed to replicate it locally.

Turns out Symfony handles both validations in different ways:

So given the current state the filter is not compatible, because it operates on an object level. The solution would be to attach the filter to the field the same way you attach the validators. However this involves a lot more digging into the Form component, so i have to take a look before i make a call on this.

In your case i would attach it all to the class, if you need to decide if you should or not validate it, then you better bet is to use validation groups. But mismatching local validations and annotated ones may not be a good idea.

ruudk commented 10 years ago

Thanks for digging into it.

I could indeed use the Annotation filters but in 1 case I wanted to add some Regex validation to the form based on other data.

rdohms commented 10 years ago

@ruudk ok that is a valid case. I would then suggest looking into data Transformers for this.

I'll close this issue and open a new one so i can look into offering field-attached filters.