Skip to main content
The package provides custom validation rules to ensure money amounts and currency codes in your HTTP requests are valid and meet specific criteria.

Available Rules

MoneyRule

Validates that an input is a valid money amount with optional min/max constraints.
use Devhammed\LaravelBrickMoney\Rules\MoneyRule;

return [
    'price' => ['required', new MoneyRule(min: 0, max: 1000)],
];

Parameters

min
Money|BigNumber|int|float|string|null
default:"null"
The minimum allowed amount. Validation fails if the value is less than this
max
Money|BigNumber|int|float|string|null
default:"null"
The maximum allowed amount. Validation fails if the value is greater than this
currency
string | null
default:"null"
The currency code to use when validating the amount
minor
bool | null
default:"null"
Whether the input value is in minor units (cents)
context
Context | null
default:"null"
The Brick\Money\Context instance to use for validation
roundingMode
RoundingMode
default:"RoundingMode::UNNECESSARY"
The rounding mode to apply when validating the amount

CurrencyRule

Validates that an input is a valid currency code from the configured currencies.
use Devhammed\LaravelBrickMoney\Rules\CurrencyRule;

return [
    'currency' => ['required', new CurrencyRule()],
];
This rule checks if the input matches a valid currency code from your config/brick-money.php configuration file.

Usage Examples

Basic Validation

namespace App\Http\Requests;

use Devhammed\LaravelBrickMoney\Rules\MoneyRule;
use Devhammed\LaravelBrickMoney\Rules\CurrencyRule;
use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'price' => [
                'required',
                new MoneyRule(min: 0, max: 1000),
            ],
            'currency' => [
                'required',
                new CurrencyRule(),
            ],
        ];
    }
}

Min/Max Validation

Enforce minimum and maximum amounts:
use Devhammed\LaravelBrickMoney\Rules\MoneyRule;

return [
    'price' => [
        'required',
        new MoneyRule(
            min: 1,        // Minimum $1.00
            max: 10000,    // Maximum $10,000.00
        ),
    ],
];
The min and max values are compared against the money amount using the same currency context.

Currency-Specific Validation

Validate amounts in a specific currency:
use Devhammed\LaravelBrickMoney\Rules\MoneyRule;

return [
    'price' => [
        'required',
        new MoneyRule(
            min: 0,
            max: 1000,
            currency: 'USD',  // Validate as USD
        ),
    ],
];

Minor Units Validation

Validate amounts provided in minor units (cents):
use Devhammed\LaravelBrickMoney\Rules\MoneyRule;

return [
    'price_cents' => [
        'required',
        new MoneyRule(
            min: 100,      // Minimum 100 cents ($1.00)
            max: 100000,   // Maximum 100,000 cents ($1,000.00)
            currency: 'USD',
            minor: true,   // Treat as cents
        ),
    ],
];

Dynamic Currency Validation

Validate money amounts with dynamic currencies based on other request fields:
namespace App\Http\Requests;

use Devhammed\LaravelBrickMoney\Rules\MoneyRule;
use Devhammed\LaravelBrickMoney\Rules\CurrencyRule;
use Illuminate\Foundation\Http\FormRequest;

class CreateTransactionRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'currency' => [
                'required',
                new CurrencyRule(),
            ],
            'amount' => [
                'required',
                new MoneyRule(
                    min: 0,
                    currency: $this->input('currency'),
                ),
            ],
        ];
    }
}

Complete Example

namespace App\Http\Requests;

use Devhammed\LaravelBrickMoney\Rules\MoneyRule;
use Devhammed\LaravelBrickMoney\Rules\CurrencyRule;
use Illuminate\Foundation\Http\FormRequest;

class UpdatePricingRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'base_price' => [
                'required',
                'numeric',
                new MoneyRule(
                    min: 10,
                    max: 100000,
                    currency: 'USD',
                ),
            ],
            'discount_price' => [
                'nullable',
                'numeric',
                new MoneyRule(
                    min: 0,
                    max: 100000,
                    currency: 'USD',
                ),
            ],
            'currency' => [
                'required',
                new CurrencyRule(),
            ],
            'tax_amount' => [
                'required',
                'integer',
                new MoneyRule(
                    min: 0,
                    currency: 'USD',
                    minor: true,  // Amount in cents
                ),
            ],
        ];
    }
    
    public function messages(): array
    {
        return [
            'base_price.required' => 'A base price is required.',
            'currency.required' => 'Please select a valid currency.',
        ];
    }
}

Validation Messages

The package provides translatable validation messages:

MoneyRule Messages

  • brick-money::validation.invalid_money - Displayed when the value cannot be converted to a valid money amount
  • brick-money::validation.min_money - Displayed when the value is less than the minimum (includes :min parameter)
  • brick-money::validation.max_money - Displayed when the value is greater than the maximum (includes :max parameter)

CurrencyRule Messages

  • brick-money::validation.invalid_selection - Displayed when the currency code is not in the configured currencies list

Custom Messages

Override validation messages in your form request:
public function messages(): array
{
    return [
        'price.required' => 'The price field is required.',
        'currency.required' => 'Please select a valid currency.',
    ];
}

Implementation Details

MoneyRule Source

MoneyRule.php
namespace Devhammed\LaravelBrickMoney\Rules;

use Brick\Math\BigNumber;
use Brick\Math\RoundingMode;
use Brick\Money\Context;
use Closure;
use Devhammed\LaravelBrickMoney\Money;
use Illuminate\Contracts\Validation\ValidationRule;

class MoneyRule implements ValidationRule
{
    public function __construct(
        public Money|BigNumber|int|float|string|null $min = null,
        public Money|BigNumber|int|float|string|null $max = null,
        public ?string $currency = null,
        public ?bool $minor = null,
        public ?Context $context = null,
        public RoundingMode $roundingMode = RoundingMode::UNNECESSARY
    ) {
        //
    }

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        try {
            $money = money($value, $this->currency, $this->minor, $this->context, $this->roundingMode);

            if ($this->min !== null && $money->isLessThan($this->min)) {
                $fail(__('brick-money::validation.min_money'))->translate([
                    'min' => $this->min,
                ]);
            }

            if ($this->max !== null && $money->isGreaterThan($this->max)) {
                $fail(__('brick-money::validation.max_money'))->translate([
                    'max' => $this->max,
                ]);
            }
        } catch (Throwable $e) {
            report($e);
            $fail(__('brick-money::validation.invalid_money'))->translate();
        }
    }
}

CurrencyRule Source

CurrencyRule.php
namespace Devhammed\LaravelBrickMoney\Rules;

use Closure;
use Devhammed\LaravelBrickMoney\Currency;
use Illuminate\Contracts\Validation\ValidationRule;

class CurrencyRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (! (is_string($value) && array_key_exists(mb_strtoupper($value), Currency::currencies()))) {
            $fail(__('brick-money::validation.invalid_selection'))->translate();
        }
    }
}

Error Handling

The MoneyRule catches any exceptions during validation and reports them while returning a validation failure. This ensures:
  • Invalid numeric formats are caught
  • Currency conversion errors are handled
  • Context-related errors don’t break the validation flow
try {
    $money = money($value, $this->currency, $this->minor, $this->context, $this->roundingMode);
    // ... validation logic
} catch (Throwable $e) {
    report($e);  // Log the error
    $fail(__('brick-money::validation.invalid_money'))->translate();
}

Next Steps

Request Macros

Extract Money and Currency from requests

JSON Serialization

Customize JSON output format

Build docs developers (and LLMs) love