Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NIP/TIN validator constraint bundle for Symfony >=6.1 applications.

License

NotificationsYou must be signed in to change notification settings

Kreyu/nip-validator-bundle

Repository files navigation

Latest Stable VersionBuild Status

Validate the tax identification numbers with configurable validation constraint.

Requirements

  • Symfony validator >=6.1
  • PHP >=8.1

For PHP 7.0 & Symfony 3.4, look at v1.x version.

Installation

To download the bundle, require it using the Composer:

$ composer require kreyu/nip-validator-bundle

Usage

Like with all Symfony validation constraints, you can apply it by using annotations:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip()     */private$nip;}

or by using attributes:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{    #[Assert\Nip]private$nip;}

or by using YAML:

App\Entity\Company:properties:nip:            -Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip:~

or by using XML:

<?xml version="1.0" encoding="UTF-8" ?><constraint-mappingxmlns="http://symfony.com/schema/dic/constraint-mapping"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">    <classname="App\Entity\Company">        <propertyname="nip">            <constraintname="Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip"/>        </property>    </class></constraint-mapping>

or by using PHP:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip;useSymfony\Component\Validator\Mapping\ClassMetadata;class Company{private$nip;publicstaticfunctionloadValidatorMetadata(ClassMetadata$metadata)    {$metadata->addPropertyConstraint('nip',newNip());    }}

Pattern validation

By default, the only accepted format is a string of characters without the prefix nor dashes.

Usage of dashes

If you wish to allow or require the usage of the dashes, use theallowDashes andrequireDashes options:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     allowDashes=true,     *     requireDashes=true     * )     */private$nip;}

Both options are set tofalse by default.
Setting therequireDashes option totrue ignores the value of theallowDashes, as the number without dashes is no longer valid.
The allowed formats with dashes are following:

  • XX-XXX-XXX-XX, e.g.34-208-769-99
  • XXX-XXX-XX-XX, e.g.342-087-69-99

Usage of prefix

If you wish to allow or require the usage of the prefix, use theallowPrefix andrequirePrefix options.
Additionally, it is possible to modify the length of the prefix, using theprefixLength option.

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     allowPrefix=true,     *     requirePrefix=true,     *     prefixLength=2     * )     */private$nip;}

BothallowPrefix andrequirePrefix options are set tofalse by default, andprefixLength defaults to2.
Prefixes are not followed with the space:

  • PPXXXXXXXXXX, e.g.PL3420876999

Customization of the pattern violation message

It is possible to customize the pattern violation message using thepatternMessage option:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     patternMessage="This is not a valid NIP number."     * )     */private$nip;}

You can use the following parameters in this message:

ParameterDescription
{{ value }}The current (invalid value)
{{ pattern }}The regular expression pattern used in the validation

Usage of a custom regular expression

If the default functionality does not meet your needs, consider using thepattern option:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     pattern="/^(\d{2}.\d{3}.\d{3}.\d{2})$/"     * )     */private$nip;}

By default this option is equalsnull, and setting it to any other value ignores theallowDashes,requireDashes,allowPrefix andrequirePrefix options.

Checksum validation

By default, the checksum is being validated. If you wish to disable this feature, set thechecksum option tofalse:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     checksum=false     * )     */private$nip;}

Customization of the checksum violation message

It is possible to customize the checksum violation message using thechecksumMessage option:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     checksumMessage="This is not a valid NIP number."     * )     */private$nip;}

You can use the following parameters in this message:

ParameterDescription
{{ value }}The current (invalid value)

Usage of a callable normalizer

It is possible to define the PHP callable to apply on the value before the validation, using thenormalizer option:

<?phpnamespaceApp\Entity;useKreyu\Bundle\NipValidatorBundle\Validator\ConstraintsasAssert;class Company{/**     * @Assert\Nip(     *     normalizer="trim"     * )     */private$nip;}

License

The MIT License (MIT). Please seelicense file for more information.

About

NIP/TIN validator constraint bundle for Symfony >=6.1 applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp