Validation and templates

laravel-rust-iban Alpha Laravel for Rust

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.

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.

MethodThe 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. 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:

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:

use laravel_rust_iban::IbanServiceProvider;

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

It adds four filters:

FilterOutput for be68 5390 0754 7034Output for invalid input
iban_humanBE68 5390 0754 7034empty string
iban_machineBE68539007547034empty string
iban_obfuscatedBE** **** **** 7034empty string
iban_validtruefalse
{% 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.