Installation
Get started with mdg-utils for Symfony
mdgascar/mdg-utils-bundle adds validation constraints, Twig filters and Doctrine types on top of the PHP package. Symfony 7.4 LTS and 8, PHP 8.3+.
Installing in a Symfony app
Section titled “Installing in a Symfony app”-
Require the bundle
Section titled “Require the bundle”Terminal composer require mdgascar/mdg-utils-bundleWith Symfony Flex the bundle is enabled for you; otherwise add
Mdgascar\MdgUtilsBundle\MdgUtilsBundle::class => ['all' => true]toconfig/bundles.php. It brings inmdgascar/mdg-utils, so every class from Using Composer works too. -
Validate input
Section titled “Validate input”src/Dto/OrderInput.php use Mdgascar\MdgUtilsBundle\Validator\AriaryAmount;use Mdgascar\MdgUtilsBundle\Validator\MalagasyPhone;use Symfony\Component\Validator\Constraints as Assert;final class OrderInput{#[Assert\NotBlank, MalagasyPhone]public ?string $phone = null;#[Assert\NotBlank, AriaryAmount]public ?string $total = null;}MalagasyPhonepasses for034 12 345 67or+261 34 12 345 67and fails for031 12 345 67.AriaryAmountpasses forAr 1 500,1 500,50or1500from JSON. Messages come in English and French. -
Store value objects with Doctrine
Section titled “Store value objects with Doctrine”src/Entity/Order.php use Doctrine\ORM\Mapping as ORM;use Mdgascar\MdgUtils\Ariary\Ariary;use Mdgascar\MdgUtils\Phone\PhoneNumber;#[ORM\Column(type: 'mg_phone')] // stored as +261341234567public ?PhoneNumber $phone = null;#[ORM\Column(type: 'mg_ariary', precision: 20, scale: 2)]public ?Ariary $total = null; -
Print values in Twig
Section titled “Print values in Twig”templates/order/show.html.twig {{ order.total|ariary }} {# Ar 1 500 #}{{ order.phone|mg_phone }} {# 034 12 345 67 #}{{ 2026|mg_words }} {# enina amby roapolo sy roa arivo #}{{ order.createdAt|mg_date }} {# 27 Novambra 2026 #}mg_phoneandmg_datetake a format name:mg_phone('international'),mg_date('numeric'). Each filter prints nothing fornull.
Configuration
Section titled “Configuration”Only Ariary amounts have options.
mdg_utils: ariary: grouping: space # space, dot or comma symbol_position: before # before or after decimal: comma # comma or dotA wrong value fails when the container is built, with the option named.