bodokaiser
bodokaiser

Reputation: 15742

Custom Validator doesn't output error message

I have installed a custom validator which checks if the generated slug is unique.

Now I am testing the validator and it seems that the validator works (form doesn't get persisted) but I don't get an error message...

class Unique extends Constraint
{
public $message = 'The value of "%property%" already exists.';
public $property;

public function getDefaultOption()
{
    return 'property';
}

public function getRequiredOptions()
{
    return array('property');
}

public function validatedBy()
{
    return 'loc_article_validator_unique_alias';
}

public function getTargets()
{
    return self::CLASS_CONSTRAINT;
}

}

The form errors are rendered through {{ form_rest(form) }} in twig

So I found the issue. The Issue was that that Custom Constraints errors can't get rendered over foreach. They have to get rendered through

{{ form_errors(form) }}

My remaining questions are now:

1.) How can I render the Custom Constrain Errors like all other errors?

2.) Why does the Custom class extending Constrain requires an alias of the CustomValidator service?

Upvotes: 2

Views: 671

Answers (1)

room13
room13

Reputation: 1922

By these lines

public function getTargets()
{
    return self::CLASS_CONSTRAINT;
}

You make the constraint a class constraint wich means the errors will show up on top of the whole form and not next to the field.

Try defining it as property constraint

public function getTargets()
{
    return self::PROPERTY_CONSTRAINT;
}

If this does not help, please post your validation definition and the form builder code that generates the form.

Upvotes: 1

Related Questions