Banned Scripts¶
Validates that a given string does not contain characters of the given script or scripts.
A Script is the Unicode term given to what is commonly called a writing system. Well known examples include the Greek, Arabic, Cyrillic and Han scripts.
The validator can fail on a minimum percentage or character count in the given value. With the default percentage of 0% the validator will fail if it encounters any character in any of the configured scripts.
Can be applied to properties or methods.
Basic Usage¶
// src/Entity/Participant.php
namespace App\Entity;
use Omines\AntiSpamBundle\Type\Script;
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\BannedScripts(Script::Cyrillic));
}
}
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.
maxPercentage
¶
type: int
default: 0
Validation will fail if the given value contains more characters in the configured scripts than configured.
Ignored if set to 0
and maxCharacters
is also set.
maxCharacters
¶
type: int|null
default: null
Validation will fail if the given value contains more characters in the configured scripts than configured.
Set to null
to disable failing on absolute character count.
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.
scripts
¶
type: array|string|Script
An array of Script
values, or strings that can be parsed into a valid Script
value. Scalars are wrapped in
an array.
For valid values refer to Script.php
in the sources.
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.