1
votes

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!

2
I should have mentioned that you should use the built in Length validator rather than writing your own. I'd also recommend the Regex validator. symfony.com/doc/current/reference/constraints/Regex.htmlMatt

2 Answers

0
votes

Looks like the regex is wrong

preg_match('/^[a-z\d_]$/i', $value, $matches)

Matches any single character in the set [a-z\d_]

Notice the output here: http://regex101.com/r/eO8bF0

If you fix it so you are matching 0 or more characters form that set (so adding the *), it should work

preg_match('/^[a-z\d_]*$/i', $value, $matches)

http://regex101.com/r/rC4qO3


Edit To answer your other question

{{ form_errors(form) }}

This will show errors for the form itself, and errors that have bubbled to the form. See the documentation on error bubbling http://symfony.com/doc/current/reference/forms/types/text.html#error-bubbling

{{ form_errors(form.some_field) }}

That will show errors for the specific field.

0
votes

For problems not I discovered, the validators did not return errors for all fields of the form and as I said when I asked, some errors saw them with form_errors(form.widget) and others with form_errors(form) I solved my problem (but I don't know if this is the best way) using the Validation Service and returning the erros to twig.

In the Action:

$form = $this->createCreateForm($entity);
$form->handleRequest($request);

if ($form->isValid())
{
    # Magic code :D
}

return array(
    'entity' => $entity,
    'form'   => $form->createView(),
    'errors' => $this->get('validator')->validate($form) # Here's the magic :D
);

And in twig template:

{% if errors is defined %}
    <ul>
        {% for error in errors %}
            <li>
                {{ error.message }}
            </li>
        {% endfor %}
    </ul>
{% endif %}

Thanks for help me :D

PD: I decided not to use error_bubbling to not modify each form field.