Skip to content

URL count

Validates that a given string contains at most a defined number of URLs, or a limited amount of duplicated URLs.

Can be applied to properties or methods.

How strict is the detection of URLs?

URL detection is loose on purpose, and will also flag “lame attempts” that are not valid, while at the same time trying to keep the chance of false positives as low as possible. Spambots are not known to strictly adhere to internet standards so being really strict would only reduce effectiveness.

Basic Usage

namespace App\Entity;

use Omines\AntiSpamBundle\Validator\Constraints as Antispam;

class Message
{
    #[Antispam\UrlCount(max: 3, maxIdentical: 1]
    protected string $content;
}
# config/validator/validation.yaml
App\Entity\Message:
    properties:
        content:
            - UrlCount:
                max: 3
                maxIdentical: 1
// src/Entity/Participant.php
namespace App\Entity;

use Omines\AntiSpamBundle\Validator\Constraints as Antispam;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Message
{
// ...

    public static function loadValidatorMetadata(ClassMetadata $metadata): void
    {
        $metadata->addPropertyConstraint('content', new Antispam\UrlCount(max: 3, maxIdentical: 1));
    }
}

Null values are always valid

As with most of Symfony’s own constraints, null is considered a valid value. This is to allow the use of optional values. If the value is mandatory, a common solution is to combine this constraint with NotBlank.

Options

groups

type: array|string default: null

It defines the validation group or groups of this constraint. Read more about validation groups.

max

type: int|null default: 0

Validation will fail if the given value contains more URLs than configured.

Set to null to disable failing on URL count altogether.

maxIdentical

type: int|null default: null

Validation will fail if any single URL in the given value occurs more often than configured.

The default null setting disables counting URL repetition.

passive

type: bool default: null, defaulting to bundle configuration

When in passive mode the constraint will not generate a validation error, but still dispatch its regular events.

With default bundle configuration passive mode is disabled.

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

stealth

type: bool default: null, defaulting to bundle configuration

With stealth mode disabled the validator will generate a verbose error, similar to Symfony built-in constraints, explaining for precisely which reasons what rule was validated.

If stealth mode is enabled instead the validator only shows a generic error message, stating that form submission failed and the user should contact the website administrator for further assistance.

With default bundle configuration stealth mode is disabled by default when used standalone, and enabled by default when applied as part of an anti-spam profile.