improve color chooser and name validation (#2622)

This commit is contained in:
Kevin Papst
2021-06-25 16:35:16 +02:00
committed by GitHub
parent 0d5e0b3e80
commit 3d1fba650b
11 changed files with 77 additions and 40 deletions

View File

@@ -22,5 +22,6 @@ class ColorChoices extends Constraint
];
public $message = 'The given value {{ value }} is not a valid hexadecimal color.';
public $invalidNameMessage = 'The given value {{ name }} is not a valid color name for {{ color }}. Allowed are {{ max }} characters, given {{ count }}.';
public $invalidNameMessage = 'The given value {{ name }} is not a valid color name for {{ color }}. Allowed are {{ max }} alpha-numerical characters, including minus and space.';
public $maxLength = 20;
}

View File

@@ -57,12 +57,15 @@ class ColorChoicesValidator extends ConstraintValidator
return;
}
if (!\is_string($name) || 1 !== preg_match('/^[0-9a-zA-Z]{1,10}$/i', $name)) {
$name = str_replace(['-', ' '], '', $name);
$length = mb_strlen($name);
if (!\is_string($name) || $length > $constraint->maxLength || !ctype_alnum($name)) {
$this->context->buildViolation($constraint->invalidNameMessage)
->setParameter('{{ name }}', $this->formatValue($name))
->setParameter('{{ color }}', $this->formatValue($code))
->setParameter('{{ max }}', $this->formatValue(10))
->setParameter('{{ count }}', $this->formatValue(\strlen($name)))
->setParameter('{{ max }}', $this->formatValue($constraint->maxLength))
->setParameter('{{ count }}', $this->formatValue($length))
->setCode(ColorChoices::COLOR_CHOICES_NAME_ERROR)
->addViolation();
}