I need to cascade validation in symfony2 form unless for specified group.
here symfony team told that group option is not supported in Valid constraint https://github.com/symfony/symfony/issues/4893
How to do it ?
Details:
I have a User Entity has address property which is a foreign key to Address Entity. Also i have Entity called business having User as property and also Address property. I need to validate address for User but, without validating it When User is a property of Business...
Schema
Class Address {
...
}
Class User {
/**
* @Assert\Valid(groups={"user"})
*/
private $address;
}
Class Business {
/**
* @Assert\Valid(groups={"business"})
*/
private $user;
/**
* @Assert\Valid(groups={"business"})
*/
private $address;
}
So I need to validate The address inside User only for User Forms but not for Business.
Thank you
I was faced with the same problem (Symfony 3).
I have a entity UserInfo
with two fields linked to one entity Place
.
And I need to validate both fields in one case, and one field in another case.
And didn't want to move constraints into Form.
In first atemt I used a Callback constraint to check group and validate one or both fields. It was fine. But input fields in form wasn't marked as invalid. All errors was displayed at top of the form.
Then I simply created own validator. Thanks to this I can specify needed groups for each field. And all invalid input fields in form marked accordingly.
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
class ValidGroupAware extends Constraint
{
}
class ValidGroupAwareValidator extends ConstraintValidator
{
/**
* 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)
{
if (!$constraint instanceof ValidGroupAware) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ValidGroupAware');
}
$violations = $this->context->getValidator()->validate($value, [new Valid()], [$this->context->getGroup()]);
/** @var ConstraintViolation[] $violations */
foreach ($violations as $violation) {
$this->context->buildViolation($violation->getMessage())
->setParameters($violation->getParameters())
->setCode($violation->getCode())
->setCause($violation->getCause())
->setPlural($violation->getPlural())
->setInvalidValue($violation->getInvalidValue())
->atPath($violation->getPropertyPath())
->addViolation();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With