# Validation and templates (laravel-rust-iban)

> The laravel-rust-iban crate adds an IbanRule for Laravel validation and template filters that format IBANs in MiniJinja views.

- Package: laravel-rust-iban (Laravel for Rust, alpha), crates: `laravel-rust-iban`, `laravel-rust-iban-core`
- Maintenance: active; Latest release: 0.1.0 (2026-12-05); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-iban), [crates.io](https://crates.io/crates/laravel-rust-iban/0.1.0), [docs.rs](https://docs.rs/laravel-rust-iban/0.1.0), [Changelog](https://oss.bowerbird.si/iban/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-iban/blob/322f32ccaf92144d5d5727285efc84ca03fd603e/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-iban/blob/322f32ccaf92144d5d5727285efc84ca03fd603e/SECURITY.md)
- Based on: [globalcitizen/php-iban](https://github.com/globalcitizen/php-iban): Reference behavior for the independent Rust implementation.
- PHP parity: core (14 covered, 34 diverged, 11 planned of 59 abilities; 565 fixtures; follows globalcitizen/php-iban v5.03b; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/iban/docs/validation/
- Source: https://github.com/bowerbird-si/laravel-rust-iban/blob/322f32ccaf92144d5d5727285efc84ca03fd603e/docs/validation.md

The `laravel-rust-iban` crate adds an `IbanRule` for Laravel validation and template filters that format IBANs in MiniJinja views.

## The validation rule

`IbanRule` implements Laravel's `ValidationRule` and converts into a `Rule`, so it sits next to the
framework's own rules.

```rust
use laravel::validation::{Rule, Rules};
use laravel_rust_iban::IbanRule;

fn rules() -> Rules {
    Rules::new()
        .field("iban", [Rule::required(), IbanRule::new().into()])
        .field(
            "billing_iban",
            [Rule::from(IbanRule::new().countries(["BE", "NL"]).sepa_only())],
        )
        .nullable("billing_iban")
}
```

`IbanRule::new()` accepts any supported country and normalises the value the way `Iban::parse` does.
The value must satisfy every condition set by the builder.

| Method | The value must… |
| --- | --- |
| `strict()` | Already be in electronic format: upper-case letters and digits, no spaces |
| `countries(["BE", "NL"])` | Have one of these prefixes, in any case |
| `sepa_only()` | Belong to a SEPA country |
| `eu_only()` | Belong to an EU member state |
| `national_check_digits()` | Pass the [national check](https://oss.bowerbird.si/iban/docs/national-check-digits/). `Invalid` and `Unsupported` both fail. |
| `message("…")` | (Replaces the failure message) |

Laravel fills in `:attribute` in the default message, `The :attribute must be a valid IBAN.` It is
also available as the constant `DEFAULT_MESSAGE`.

Laravel skips this rule for missing and blank values, including blank strings longer than 1024
bytes. Add `required` for mandatory fields. If you store the original input and need a byte limit,
check `str::len()` before storing it, including for blank strings. Nonblank strings must pass the
parser's 1024-byte cap. Non-string values other than null fail with the rule's message.

`national_check_digits()` rejects every country without a national check. Combine it with
`countries` to accept only accounts you can check, for example Belgian accounts for direct debits:

```rust
use laravel_rust_iban::IbanRule;

fn main() {
    let _rule = IbanRule::new()
        .countries(["BE"])
        .national_check_digits()
        .message("Enter a Belgian bank account number.");
}
```

To store the electronic form, parse the validated
value with `Iban::parse` in your handler.

## Template filters

With the `minijinja` feature, register the service provider:

```rust
use laravel_rust_iban::IbanServiceProvider;

fn main() {
    let _app = laravel::Application::configure()
        .with_providers(|providers| {
            providers.add(IbanServiceProvider);
        })
        .create();
}
```

It adds four filters:

| Filter | Output for `be68 5390 0754 7034` | Output for invalid input |
| --- | --- | --- |
| `iban_human` | `BE68 5390 0754 7034` | empty string |
| `iban_machine` | `BE68539007547034` | empty string |
| `iban_obfuscated` | `BE** **** **** 7034` | empty string |
| `iban_valid` | `true` | `false` |

```jinja
{% if account.iban | iban_valid %}
  Paid from {{ account.iban | iban_obfuscated }}
{% endif %}
```

Without the feature the provider registers nothing, so you can keep it in your provider list either way.
