-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Form] Using form data to set constraints options #7444
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
Configuring the validation with the values looks wrong, as the validation is about checking the value. What you probably need is the constraint comparing 2 properties of an object, which are implemented in #790 and may be part of Symfony 2.3. And these cannot be implemented with Length or Range as the logic is totally diffeent (it is not the same rules) |
I agree that this solution solves the second example, but how about the |
Currently, I need create a class validator, inject public function validate($value, Constraint $constraint)
{
if (!$value instanceof OptionTextInterface) {
throw new UnexpectedTypeException($value, 'Acme\ExampleBundle\Model\OptionTextInterface');
}
$option = $value;
$maxLength = $option->getMaxLength();
$minLength = $option->getMinLength();
// minLength cannot be great than maxLength
if ($minLength > 0 && $maxLength > 0) {
$rangeConstraint = new Range(array(
'max' => $maxLength,
'maxMessage' => $constraint->minLengthLogicalMessage,
));
$this->validateValue($minLength, 'minLength', $rangeConstraint);
}
// The default value must respect the minLength and maxLength constraints
if (($minLength > 0 || $maxLength > 0) && ($defaultValue = $option->getDefaultValue())) {
$lengthConstraint = new Length(array(
'min' => $minLength,
'max' => $maxLength,
'minMessage' => $constraint->defaultValueMinLengthMessage,
'maxMessage' => $constraint->defaultValueMaxLengthMessage,
));
$this->validateValue($defaultValue, 'defaultValue', $lengthConstraint);
}
}
private function validateValue($value, $property, $constraint)
{
$errors = $this->validator->validateValue($value, $constraint);
if (count($errors) > 0) {
$this->context->addViolationAt($property, $errors[0]->getMessage());
return false;
}
return true;
} It could be addressed with something like: <class name="Acme\ExampleBundle\Entity\OptionText">
<property name="minLength">
<constraint name="Range">
<option name="max">
<!-- PropertyPath style -->
<propertyValue>maxLength</propertyValue>
</option>
</constraint>
</property>
<property name="defaultValue">
<constraint name="Length">
<option name="min">
<propertyValue>minLength</propertyValue>
</option>
<option name="max">
<propertyValue>maxLength</propertyValue>
</option>
</constraint>
</property>
</class> Rules:
|
What about adding a use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Reference;
/**
* @Range(min = @Reference("min"), max = @Reference("max"))
*/
private $value;
private $min;
private $max |
You have got! It is exactly what I was talking about, and the most important: it solves two problems with one simple and efficient solution. |
@bschussek but any valdiator would have to support the case of And this would require changing the XML and YAML loaders too as they are currently unable to handle this (see the issue with Optional and Required in the Collection constraint) |
Closing in favor of #7726. |
I've a model with the fields
defaultValue
,minLength
andmaxLength
. The propertydefaultValue
must be validated byConstraints\Length
, but themin
andmax
options depends ofminLength
andmaxLength
values. Furthermore, I would like to validate ifminLength
is less thanmaxLength
usingConstraints\Range
. Once there is specialized validators to do this job, I wouldn't like to use the callback constraint.I believe that is no way to do that currently, but is a very usefull feature.
The text was updated successfully, but these errors were encountered: