[2.0.0 Beta 2] Doesn't filter before validation
Closed this issue · 5 comments
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.
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;
@ruudk ok, I managed to replicate it locally.
Turns out Symfony handles both validations in different ways:
- annotation based is executed against the object (data_class), where filters are executed, and executed before validation, hence it works.
- fields based constraints are actually run against raw form data, not the parsed object, as this is designed to run in forms without
data_class
. Hence the data has not been directed at the filter.
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.
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.