I have read about how to validate the value of a field depending in the value of another one. But all of these are in symfony2.1 which use the function "isValid()". That is not my case because i am using "validate()" function.
Here is my code :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('type');
$builder->addValidator(new CallbackValidator(function(FormInterface $form){
$validator = new IntegerValidator();
$validator->validate($description->getData(),new Integer());
}));
}
However when i make a submit, i am always getting this error:
Call to a member function addViolation() on a non-object in ..\IntegerValidator.php
These are the codes of my 2 classes.
class IntegerValidator extends ConstraintValidator{
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^[0-9]+$/', $value, $matches)) {
$this->context->addViolation($constraint->message, array('%string%' => $value));
}
}
}
And this is my constraint class
/**
* @Annotation
*/
class Integer extends Constraint{
public $message = 'The string "%string%" contains an illegal character: it can only be an integer.';
}
How could i implement this?. Thanks a lot for your help.