Custom constraint embedding existing constraints
Closed this issue · 5 comments
Hi,
For a very specific case for which I need to validate a field only if an associated checkbox is checked, I've created a custom constraint embedding existing constraints and using my whole object as $value. I don't know if it's the way it should be but it's working as expected.
Here the code of my validator.
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof ValidScheduledEndDate) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ValidScheduledEndDate');
}
if ($value->getIsEndDateScheduled()) {
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->getValidator();
$constraintNotBlank = new NotBlank();
$constraintNotBlank->message = $constraint->messageNotBlank;
$constraintDate = new Date();
$constraintDate->message = $constraint->messageDate;
$constraintBeforeNow = new BeforeNow();
$constraintBeforeNow->message = $constraint->messageBeforeNow;
$violations = $validator
->inContext($this->context)
->atPath('scheduledEndDate')
->validate($value->getScheduledEndDate(), array(
$constraintNotBlank,
$constraintDate,
$constraintBeforeNow
))
;
}
}
Do you know if there is a way to create a custom rule mapper for this specific case ?
Thanks.
Hi,
So there is a way to do this but it's not very easy at the moment.
To start of you will need to implement a custom RuleCondition
(similar to the FieldDependency
) to check that a value is set on a related field (I'm assuming that getIsEndDateScheduled
is dynamic in the form).
After that a custom FormRuleProcessor
is needed that will check for the ValidScheduledEndDate
constraint and that will add the rules form the related fields.
You probably also what to implement a ConstraintMapper
for the BeforeNow
constraint.
I hope this helps. I'm willing to add a FieldValueDependency
condition to the lib when I have time to make it easier.
Thanks. I'll try it when I'll have the time.
I'll dig into your code to understand the behaviour of the class you
recommend me to implement.
2015-08-10 2:49 GMT+02:00 Warnar Boekkooi notifications@github.com:
Hi,
So there is a way to do this but it's not very easy at the moment.
To start of you will need to implement a custom RuleCondition
https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Form/RuleCondition.php
(similar to the FieldDependency
https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Form/Rule/Condition/FieldDependency.php)
to check that a value is set on a related field (I'm assuming that
getIsEndDateScheduled is dynamic in the form).After that a custom FormRuleProcessor
https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Form/FormRuleProcessorInterface.php
is needed that will check for the ValidScheduledEndDate constraint and
that will add the rules form the related fields.You probably also what to implement a ConstraintMapper
https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Form/Rule/ConstraintMapperInterface.php
for the BeforeNow constraint.I hope this helps. I'm willing to add a FieldValueDependency condition to
the lib when I have time to make it easier.—
Reply to this email directly or view it on GitHub
#20 (comment)
.
I have added a simple example for you on how you could solve your problem 😄
If you need any more help please reopen the issue.
I hadn't seen your example. It will be very useful !
Thank you very much.
@BboyKeen the example is in commit c9ad527 this uses a form_rule_processor
.
You can in some cases also use a form_rule_constraint_mapper
see for example https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Form/Rule/Mapping/FileRule.php & https://github.com/boekkooi/JqueryValidationBundle/blob/master/src/Resources/config/form_rule_mappers.yml