Description
I am wondering if there is a way we can get an option like property_path
added to the callback validator? Here's a good example of why.
Say you have a Company object with a numberOfRecruiters
field, and this field should only be validated in the case that Company::getType() is equal to “recruiter” for example.
So I add a callback validator, so that this validation only takes place for recruiter type companies, like this:
/**
* @Assert\Callback
* @param ExecutionContextInterface $context
*/
public function isNumberOfRecruitersValid(ExecutionContextInterface $context)
{
if ($this->companyType === Company::TYPE_RECRUITER) {
$notBlank = new NotBlankValidator();
$notBlank->initialize($context);
$notBlank->validate($this->numberOfRecruiters, new NotBlank());
}
}
Being the good DRY coder like I am, since I am only looking to verify that the field is not blank, I piggy back on the existing NotBlankValidator, rather than rewriting the validation checking code and calling addViolationAt manually.
However, when this is done, the NotBlank validator adds a violation, but it does not get mapped to the specific numberOfRecruiters
property. The error appears on the entire object itself (or on the top of the form when using the Form component).
I think it would be nice if we could specify an option, so we can recieve an ExecutionContextInterface with the property_path already set in our callback method. To do this we could annotate the callback method like so @Callback(property_path="numberOfRecruiters")
.
Otherwise, if we want to use built in validation classes conditionally in our custom callback methods, we will need to repeat code, or have the errors mapped improperly.
Thoughts?