I created multiple validators using de oficial documentation and all of them works fine, but only when I use separately. In a bundle I defined this:
# Resources/config/validation.ynl
SF\SomeBundle\Entity\SomeEntity:
properties:
name:
- NotBlank: ~
- SF\UtilsBundle\Validator\Constraints\ContainsAlphanumeric: ~
- SF\UtilsBundle\Validator\Constraints\MinLength: ~
ContainstAlphanumeric Class validator:
if (!preg_match('/^[a-z\d_]$/i', $value, $matches)) {
$this->context->addViolation($constraint->message, array('%string%' => $value));
}
MinLength Class validator
$min = 5;
if( strlen($value) < $min )
{
$this->context->addViolation($constraint->message, array('%string%' => $value, '%min_length%' => $min));
}
So, when I submit a form and the input has the value "q", the validator MinLength returns a length error, but if the same input has the value "qwerty", the validator ContainsAlphanumeric returns an illegal character message.
Any ideas?
Edit:
I changed Resources/config/validation.yml file to use the native SF2 Contraints length validator:
properties:
name:
- NotBlank: ~
- Length: { min: 5, minMessage: "El nombre debe tener almenos {{ limit }} caracteres." }
- SF\UtilsBundle\Validator\Constraints\ContainsAlphanumeric: ~
And I descover a new behaviour: Some errors are displayed in twig templates with
{{ form_errors(form) }}
and other errors using
{{ form_errors(form.some_field) }}
This is weird!
Length
validator rather than writing your own. I'd also recommend theRegex
validator. symfony.com/doc/current/reference/constraints/Regex.html – Matt