MoneyBundle

Documentation

Form

The MoneyBundle provides support for forms using a Money instance as a data object by providing an alternative MoneyType form type for the Symfony Form component.

With the Form component installed, the form type will automatically be registered to your application and available for use in your form classes.

Note, this form type uses the same block prefix as the native MoneyType provided by the Form component; this allows the form type provided by this bundle to act as a drop-in replacement from a templating perspective.

<?php

namespace App\Form;

use BabDev\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class ProductForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'name',
                TextType::class,
                [
                    'label' => 'app.form.label.product_name',
                ]
            )
            ->add(
                'price',
                MoneyType::class,
                [
                    'label' => 'app.form.label.price',
                ]
            )
        ;
    }
}

You can also use the validator integration to define constraints on your form types.

<?php

namespace App\Form;

use BabDev\MoneyBundle\Form\Type\MoneyType;
use BabDev\MoneyBundle\Validator\Constraints\MoneyGreaterThanOrEqual;use Money\Currency;use Money\Money;use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class ProductForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'name',
                TextType::class,
                [
                    'label' => 'app.form.label.product_name',
                ]
            )
            ->add(
                'price',
                MoneyType::class,
                [
                    'label' => 'app.form.label.price',
                    'currency' => new Currency('EUR'),
                    'constraints' => [
                        new MoneyGreaterThanOrEqual(Money::EUR(0)),
                    ]
                ]
            )
        ;
    }
}

Options

The MoneyType form type largely accepts the same options as the form type from the Form component:

  • currency - Specifies the currency that the money is being specified in. Unlike the Form component, this must be provided as a Money\Currency instance instead of a string. If not specified, the default currency from the babdev_money.default_currency configuration node is used.
  • grouping - This value is used internally as the NumberFormatter::GROUPING_USED value when using PHP's NumberFormatter class. This should be a boolean value and defaults to false. Note, this option cannot be used with the html5 option.
  • rounding_mode - If a submitted number needs to be rounded (based on the scale option), you have several configurable options for that rounding. This should be one of the NumberFormatter::ROUNDING_* constants and defaults to NumberFormatter::ROUND_HALFUP.
  • html5 - If set to true, the HTML input will be rendered as a native HTML5 number input. This should be a boolean value and defaults to false.
  • scale - The number of decimal places for the amount. This should be an integer and defaults to the number of decimal places used by the currency (for example, 2 for USD and 0 for JPY). It cannot be greater than the number of decimal places used by the currency, as the extra digits could not be stored in a Money\Money instance; it can be lower to round amounts to fewer decimal places.

Type Guessing

The bundle registers a form type guesser which uses the MoneyType form type for properties declared with the Money\Money type. This allows you to add these fields without specifying a type, and makes Money\Money properties work with the #[AsFormType] and #[FormField] attributes introduced in Symfony 8.2.

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

final class ProductForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            // Guessed as a MoneyType field when the `price` property is declared as `Money $price` or `?Money $price`
            ->add('price')
        ;
    }
}

Only the type is guessed; options such as currency are not, so fields for amounts in a currency other than the default currency still need their currency option set. Properties declared with a union type, such as Money|int, are not guessed.

Price Ranges With The BoundsType

Symfony 8.2 introduced the BoundsType form type, which renders a lower and an upper bound using the same inner type. The MoneyType can be used as the inner type, and when the compare option is set to true, the bundle checks that the lower bound is not greater than the upper bound by comparing the two Money\Money instances.

<?php

namespace App\Form;

use BabDev\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\BoundsType;
use Symfony\Component\Form\FormBuilderInterface;

final class ProductFilterForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'price',
                BoundsType::class,
                [
                    'type' => MoneyType::class,
                    'compare' => true,
                ]
            )
        ;
    }
}

This only applies when the type option is the bundle's MoneyType; a callable passed to the compare option is always used as is. For your own form type using the MoneyType as its parent, pass a callable comparing the two amounts, such as static fn (Money $from, Money $to): int => $from->compare($to).

Both bounds use the same currency option by default. If you set a different currency for each bound with the from_options and to_options options, the bounds cannot be compared and a Money\Exception\CurrencyMismatchException is thrown when the form is submitted.