Reputation: 430
i'm using Symfony 2.8 and FOSUser bundle 2.0 for my user management. I'm trying to create custom validator for phone number field without success.
This is my code:
UserBundle/Resources/config/validation.xml
<class name="Acme\UserBundle\Entity\User">
<property name="phoneNumber">
<constraint name="Acme\UserBundle\Validator\Constraint\IsConfirmedPhonenumber">
<option name="groups">
<value>Acme</value>
</option>
</constraint>
</property>
</class>
Service definition
<service class="Acme\UserBundle\Validator\Constraint\IsConfirmedPhonenumberValidator" id="acme_user.validator.is_confirmed_phonenumber_validator">
<argument id="doctrine.orm.default_entity_manager" type="service"/>
<tag name="acme.constraint_phonenumber"/>
</service>
On my form type
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\UserBundle\Entity\User',
'intention' => 'profile',
));
}
My custom constraint
class IsConfirmedPhonenumber extends Constraint
{
public $message = 'The phonenumber "%phonenumber%" is not verified.';
public function validatedBy()
{
return "acme.constraint_phonenumber";
}
}
And validator
class IsConfirmedPhonenumberValidator extends ConstraintValidator
{
/**
* @var EntityManager
*/
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($value, Constraint $constraint)
{
$phoneVerificationRepo = $this->em->getRepository('AcmeCoreBundle:PhoneVerification');
$phoneVerificationRepo->findBy(array('phoneNumber' => $value, 'verified' => true));
if(!$phoneVerificationRepo)
{
$this->context->buildViolation($constraint->message)
->setParameter('%phonenumber%', $value)
->addViolation();
}
return;
}
}
This configuration doesn't work for me since validate method in my custom validator never gets called.
Upvotes: 0
Views: 211
Reputation: 430
I managed to fix the issue by specifying validation groups in createForm factory method in overridden editAction method of ProfileController.
$form = $formFactory->createForm(['validation_groups' => array('AcmeProfile', 'Profile')]);
Setting validation groups in config or custom FormType didn't help either since i'm overriding FOS factory and ProfileController editAction.
Upvotes: 0
Reputation: 47585
In your service definition, your tag name is wrong, it should be validator.constraint_validator
to be registered as a Validator
.
<service class="Acme\UserBundle\Validator\Constraint\IsConfirmedPhonenumberValidator" id="acme_user.validator.is_confirmed_phonenumber_validator">
<argument id="doctrine.orm.default_entity_manager" type="service"/>
<tag name="validator.constraint_validator" />
</service>
And your constraint should be configured using the id
of your validator service, therefor:
class IsConfirmedPhonenumber extends Constraint
{
public $message = 'The phonenumber "%phonenumber%" is not verified.';
public function validatedBy()
{
return "acme_user.validator.is_confirmed_phonenumber_validator";
}
}
Upvotes: 1