-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Let option values reference values in the object graph #7726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
How would you inject the ReferenceResolver ? Would the ConstraintValdiatorFactory know it and inject it by doing an instanceof check ? Or would you require defining all these valdiators as service ? |
That's a possibility. Alternatively, we could add |
yeah, it may be easier (no need to inject the resolver in constraint validators) |
@danielholmes what is not clear for you? Can we discuss about that here? Do you already read the issue #7444? For me, it's clear, but I can try to answer your questions. |
@marcospassos I hadn't seen #7444 before, yea that's all clear. It's more work than I thought it might be. It's getting late where I am and my work day is full tomorrow so I probably couldn't do it before feature freeze. If you or someone else completed this generic |
This will be postponed to 2.4. There are too many open questions for finishing this by tomorrow. |
@bschussek what are the open questions (except about the YML syntax)? I would like to work on that to keep live. Adding the |
@bschussek can it be closed now that we have expressions ? |
@stof Not unless we make it possible to execute other constraints from within the |
In fact, this can easily be done using callbacks: /**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
$validator = $context->getValidator()->inContext($context);
$validator->atPath('value')->validate($this->value, new Range(array(
'min' => $this->min,
'max' => $this->max,
)));
} I think this is simpler and superior to some magic new syntax. |
In #790 and #7444 the need to reference dynamic values in constraint options was discovered. A simple example:
Here, the values of the "min" and "max" option are determined at runtime. References can contain any valid property path.
XML example:
YAML example:
Attention: This example would be consistent with the DIC YAML syntax for service references, but it would be incompatible for options where values starting with "@" are valid expected values. Is this a problem?
PHP example:
Some implementation hints:
Class
Reference
should be an annotation but not inherit fromConstraint
(it is no constraint). It probably needs only a constructor. Additionally, we need aReferenceResolver
(andReferenceResolverInterface
, of course) that receives aPropertyAccessorInterface
in its constructor. It needs only a methodresolveReference(Reference $reference, ExecutionContextInterface $context)
that returns the value of a reference based on the current state of the execution context.All validators that support references then need the
ReferenceResolver
to resolve anyReference
instances when they find them.The text was updated successfully, but these errors were encountered: