PHPackages                             robertogallea/laravel-codicefiscale - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. robertogallea/laravel-codicefiscale

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

robertogallea/laravel-codicefiscale
===================================

Codice fiscale validation for php/laravel

2.3.1(4mo ago)60198.8k↓49.6%19[1 issues](https://github.com/robertogallea/laravel-codicefiscale/issues)1PHPPHP ^8.2CI passing

Since Mar 22Pushed 4mo ago6 watchersCompare

[ Source](https://github.com/robertogallea/laravel-codicefiscale)[ Packagist](https://packagist.org/packages/robertogallea/laravel-codicefiscale)[ GitHub Sponsors](https://github.com/robertogallea)[ RSS](/packages/robertogallea-laravel-codicefiscale/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (8)Versions (47)Used By (1)

[![Laravel Codice Fiscale](https://camo.githubusercontent.com/01a36fd69e63fba0edb5038d3e16e66f9e8911a836397118a39839f8aced61db/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230436f6469636525323046697363616c652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d726f626572746f67616c6c65612532466c61726176656c2d636f6469636566697363616c65267061747465726e3d636861726c696542726f776e267374796c653d7374796c655f31266465736372697074696f6e3d436f646963652b66697363616c652b76616c69646174696f6e2b616e642b70617273696e672b69732b612b627265657a65266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d6964656e74696669636174696f6e267769647468733d32303026686569676874733d6175746f)](https://camo.githubusercontent.com/01a36fd69e63fba0edb5038d3e16e66f9e8911a836397118a39839f8aced61db/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230436f6469636525323046697363616c652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d726f626572746f67616c6c65612532466c61726176656c2d636f6469636566697363616c65267061747465726e3d636861726c696542726f776e267374796c653d7374796c655f31266465736372697074696f6e3d436f646963652b66697363616c652b76616c69646174696f6e2b616e642b70617273696e672b69732b612b627265657a65266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d6964656e74696669636174696f6e267769647468733d32303026686569676874733d6175746f)

laravel-codicefiscale
=====================

[](#laravel-codicefiscale)

[![Author](https://camo.githubusercontent.com/bd84f242372028b300dda5beb4de8d4730d4df44685c8554a873e716659feb1c/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d617574686f72266d6573736167653d726f6267616c6c656126636f6c6f723d353041424631266c6f676f3d74776974746572267374796c653d666c61742d737175617265)](https://twitter.com/robgallea)[![Latest Version on Packagist](https://camo.githubusercontent.com/3aee563f404a627ba3d355462bb564d3d25efc58beb9473216d077b8b14c6cd7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f626572746f67616c6c65612f6c61726176656c2d636f6469636566697363616c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robertogallea/laravel-codicefiscale)[![Laravel >=6.0](https://camo.githubusercontent.com/0c9c95ea41100ab5978a5e17560b8b0463e09bb57b41eeab5892d082eea53bff/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d6c61726176656c266d6573736167653d254532253839254135362e3026636f6c6f723d666632643230266c6f676f3d6c61726176656c267374796c653d666c61742d737175617265)](https://laravel.com)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Sponsor me!](https://camo.githubusercontent.com/78d8f37e45135156aa667c76aa28893949f0d59ea84c527e854792166f2a06eb/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d53706f6e736f72266d6573736167653d254532253944254134266c6f676f3d476974487562266c696e6b3d68747470733a2f2f6769746875622e636f6d2f73706f6e736f72732f726f626572746f67616c6c6561)](https://github.com/sponsors/robertogallea)[![Packagist Downloads](https://camo.githubusercontent.com/f571faede5d7b4e8f63efed2223e3e08bb2110e0d310756ae73bba0bdf1e8d1e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f626572746f67616c6c65612f6c61726176656c2d636f6469636566697363616c65)](https://packagist.org/packages/robertogallea/laravel-codicefiscale)

laravel-codicefiscale is a package for the management of the Italian `CodiceFiscale` (i.e. tax code). The package allows easy validation and parsing of the CodiceFiscale. It is also suited for Laravel since it provides a convenient custom validator for request validation.

Laravel Version Compatibility
-----------------------------

[](#laravel-version-compatibility)

LaravelPackage12.x^2.211.x2.x10.x1.x9.x1.x8.x1.x7.x1.x6.x1.x> **Important update**: now you can dynamically load city codes from ISTAT using the non-default `IstatRemoteCSVList`city decoder.

- [Installation](#installation)
- [Configuration](#configuration)
- [Validation](#validation)
- [Utility CodiceFiscale class](#utility-codicefiscale-class)
- [Codice fiscale Generation](#codice-fiscale-generation)
- [Faker integration](#faker-integration)
- [City code parsing](#city-code-parsing)
- [Integrate your own cities](#integrate-your-own-cities)

Installation
------------

[](#installation)

Run the following command to install the latest applicable version of the package:

```
composer require robertogallea/laravel-codicefiscale:^2
```

### Laravel

[](#laravel)

In your app config, add the Service Provider to the `$providers` array *(only for Laravel 5.4 or below)*:

```
'providers' => [
   ...
   robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider::class,
],
```

The validation error messages are translated in `it` and `en` languages, if you want to add new language please send me a PR.

### Lumen

[](#lumen)

In `bootstrap/app.php`, register the Service Provider

```
$app->register(robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider::class);
```

Configuration
-------------

[](#configuration)

To customize the package configuration, you must export the configuration file into `config/codicefiscale.php`.

This can be achieved by launching the following command:

```
php artisan vendor:publish --provider="robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider" --tag="config"

```

You can configure the following parameters:

- `city-decoder`: the class used for decoding city codes (see [City code parsing](#city-code-parsing)), default to `InternationalCitiesStaticList`.
- `date-format`: the date format used for parsing birthdates, default to `'Y-m-d'`.
- `labels`: the labels used for `male` and `female` persons, defaults to `'M'` and `'F'`.

Language Files
--------------

[](#language-files)

You can customize the validation messages publishing the validation translations with this command:

```
php artisan vendor:publish --provider="robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider" --tag="lang"

```

Validation
----------

[](#validation)

To validate a codice fiscale, use the `codice_fiscale` keyword in your validation rules array

```
    public function rules()
    {
        return [
            'codicefiscale' => 'codice_fiscale',

            //...
        ];
    }
```

From version **1.9.0** you can validate your codice fiscale against other form fields to check whether there is a match or not.

You must specify all of the required fields:

- `first_name`
- `last_name`
- `birthdate`
- `place`
- `gender`

giving parameters to the `codice_fiscale` rule.

For example:

```
    public function rules()
    {
        return [
            'codicefiscale' => 'codice_fiscale:first_name=first_name_field,last_name=last_name_field,birthdate=birthdate_field,place=place_field,gender=gender_field',
            'first_name_field' => 'required|string',
            'last_name_field' => 'required|string',
            'birthdate_field' => 'required|date',
            'place_field' => 'required|string',
            'gender_field' => 'required|string|max:1',

            //...
        ];
    }
```

Validation fails if the provided codicefiscale and the one generated from the input fields do not match.

Utility CodiceFiscale class
---------------------------

[](#utility-codicefiscale-class)

A codice fiscale can be wrapped in the `robertogallea\LaravelCodiceFiscale\CodiceFiscale` class to enhance it with useful utility methods.

```
use robertogallea\LaravelCodiceFiscale\CodiceFiscale;

...
try {
    $cf = new CodiceFiscale();
    $result = $cf->parse('RSSMRA95E05F205Z');
    var_dump($result);
} catch (Exception $exception) {
    echo $exception;
}
```

In case of a valid codicefiscale it produces the following result:

```
[
  "gender" => "M"
  "birth_place" => "F205"
  "birth_place_complete" => "Milano",
  "day" => "05"
  "month" => "05"
  "year" => "1995"
  "birthdate" => Carbon @799632000 {
    date: 1995-05-05 00:00:00.0 UTC (+00:00)
  }
]
```

in case of an error, `CodiceFiscale::parse()` throws an `CodiceFiscaleValidationException`, which returns one of the defined constants with `$exception->getCode()`:

- `CodiceFiscaleException::NO_ERROR`
- `CodiceFiscaleException::NO_CODE`
- `CodiceFiscaleException::WRONG_SIZE`
- `CodiceFiscaleException::BAD_CHARACTERS`
- `CodiceFiscaleException::BAD_OMOCODIA_CHAR`
- `CodiceFiscaleException::WRONG_CODE`
- `CodiceFiscaleException::MISSING_CITY_CODE`

If you rather not want to catch exceptions, you can use `CodiceFiscale::tryParse()`:

```
use robertogallea\LaravelCodiceFiscale\CodiceFiscale;

...
$cf = new CodiceFiscale();
$result = $cf->tryParse('RSSMRA95E05F205Z');
if ($result) {
    var_dump($cf->asArray());
} else {
    echo $cf->getError();
}
```

which returns the same values as above, you can use `$cf->isValid()` to check if the codicefiscale is valid and `$cf->getError()` to get the error. This is especially useful in a blade template:

```
@php($cf = new robertogallea\LaravelCodiceFiscale\CodiceFiscale())
@if($cf->tryParse($codicefiscale))
    {{$cf->getCodiceFiscale()}}
@else
    {{$cf->getError()->getMessage()}}
@endif
```

Codice fiscale Generation
-------------------------

[](#codice-fiscale-generation)

Class `CodiceFiscale` could be used to generate codice fiscale strings from input values:

```
$first_name = 'Mario';
$last_name = 'Rossi';
$birth_date = '1995-05-05'; // or Carbon::parse('1995-05-05')
$birth_place = 'F205';      // or 'Milano'
$gender = 'M';

$cf_string = CodiceFiscale::generate($first_name, $last_name, $birth_date, $birth_place, $gender);
```

Faker integration
-----------------

[](#faker-integration)

You can generate fake codice fiscale in your factories using the provided faker extension:

```
class PersonFactory extends Factory
{

    public function definition(): array
    {
        return [
            'first_name' => $firstName = fake()->firstName(),
            'last_name' => $lastName = fake()->lastName(),
            'fiscal_number' => fake()->codiceFiscale(firstName: $firstName, lastName: $lastName),
        ];
    }
```

**Note**: you can provide some, all or none of the information required for the generation of codice fiscale (`firstName`, `lastName`, `birthDate`, `birthPlace`, `gender`)

City code parsing
-----------------

[](#city-code-parsing)

There are three strategies for decoding the city code:

- `InternationalCitiesStaticList`: a static list of Italian cities;
- `ItalianCitiesStaticList`: a static list of International cities;
- `IstatRemoteCSVList`: a dynamic (loaded from web) list of Italian cities loaded from official ISTAT csv file. Please note that the list is cached (one day by default, see config to change).
- `CompositeCitiesList`: merge the results from two `CityDecoderInterface` classes (for example `IstatRemoteCSVList` and `InternationalCitiesStaticList`) using the base `CityDecoderInterface` in the config key `codicefiscale.cities-decoder-list`.

By default, the package uses the class `InternationalCitiesStaticList` to lookup the city from the code and viceversa. However, you could use your own class to change the strategy used.

You just need to implement the `CityDecoderInterface` and its `getList()` method. Then, to use it, just pass an instance to the `CodiceFiscale` class.

For example:

```
class MyCityList implements CityDecoderInterface
{
  public function getList()
  {
    // Implementation
  }
}
```

```
...
$cf = new CodiceFiscale(new MyCityList)
...
```

Integrate your own cities
-------------------------

[](#integrate-your-own-cities)

*Note*: if you find missing cities, please make a PR!

If you want to integrate the cities list, you can use the `CompositeCitiesList` by merging the results of one of the decoders provided and a custom decoder.

For example:

```
// conf/codicefiscale.php

return [
  'city-decoder' => '\robertogallea\LaravelCodiceFiscale\CityCodeDecoders\CompositeCitiesList',

  ...

  'cities-decoder-list' => [
        '\robertogallea\LaravelCodiceFiscale\CityCodeDecoders\InternationalCitiesStaticList',
        'YourNamespace\MyCustomList',
    ]

```

where `MyCustomList` is defined as:

```
...

class MyCustomList implements CityDecoderInterface
{
  public function getList()
  {
    return [
      'XYZ1' => 'My city 1',
      'XYZ2' => 'My city 2',
    ]
  }
}

```

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance76

Regular maintenance activity

Popularity49

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 71.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~57 days

Recently: every ~92 days

Total

45

Last Release

123d ago

Major Versions

1.14.0 → 2.0.02024-06-14

PHP version history (3 changes)1.0.0PHP &gt;=7.1

1.12.0PHP &gt;=7.4

2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/818f547bcf73a82393d9014c85c90c83d760102a8d4dfe806353afb83848a901?d=identicon)[robertogallea](/maintainers/robertogallea)

---

Top Contributors

[![robertogallea](https://avatars.githubusercontent.com/u/19411470?v=4)](https://github.com/robertogallea "robertogallea (134 commits)")[![makroxyz](https://avatars.githubusercontent.com/u/2069949?v=4)](https://github.com/makroxyz "makroxyz (26 commits)")[![michelepizzi](https://avatars.githubusercontent.com/u/20208916?v=4)](https://github.com/michelepizzi "michelepizzi (10 commits)")[![trippo](https://avatars.githubusercontent.com/u/497169?v=4)](https://github.com/trippo "trippo (3 commits)")[![JBou](https://avatars.githubusercontent.com/u/4021928?v=4)](https://github.com/JBou "JBou (3 commits)")[![MoroAlberto](https://avatars.githubusercontent.com/u/21240710?v=4)](https://github.com/MoroAlberto "MoroAlberto (2 commits)")[![caiojhonny](https://avatars.githubusercontent.com/u/2778743?v=4)](https://github.com/caiojhonny "caiojhonny (2 commits)")[![wit3](https://avatars.githubusercontent.com/u/5450259?v=4)](https://github.com/wit3 "wit3 (1 commits)")[![carloeusebi](https://avatars.githubusercontent.com/u/129429172?v=4)](https://github.com/carloeusebi "carloeusebi (1 commits)")[![DavideDelNista](https://avatars.githubusercontent.com/u/59122941?v=4)](https://github.com/DavideDelNista "DavideDelNista (1 commits)")[![eleftrik](https://avatars.githubusercontent.com/u/6959298?v=4)](https://github.com/eleftrik "eleftrik (1 commits)")[![fabio-ivona](https://avatars.githubusercontent.com/u/8792274?v=4)](https://github.com/fabio-ivona "fabio-ivona (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")[![BitTheCat](https://avatars.githubusercontent.com/u/12282238?v=4)](https://github.com/BitTheCat "BitTheCat (1 commits)")

---

Tags

hacktoberfest

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/robertogallea-laravel-codicefiscale/health.svg)

```
[![Health](https://phpackages.com/badges/robertogallea-laravel-codicefiscale/health.svg)](https://phpackages.com/packages/robertogallea-laravel-codicefiscale)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k38](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3741.3M47](/packages/tencentcloud-tencentcloud-sdk-php)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

273.0k](/packages/eslazarev-wildberries-sdk)[files.com/files-php-sdk

Files.com PHP SDK

2481.1k](/packages/filescom-files-php-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
