mikesimonson/symfony-form-value-object-mapper

A library that can instantiate automatically value object mapped by Symfony forms despite having a constructor with arguments.

dev-master 2017-03-30 07:33 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:57:25 UTC


README

Build Status Code Coverage

Motivation

Value object usage with the Symfony form component. There is a great post from Webmozart explaining how to use the Symfony form component with value object. But sometimes people might get discourage to use value object because they now need to do the mapping between the object and the form manually. It can look daunting when the number of properties of the object is high.

This library intend to fix 90% of that issue by providing automatic mapping in most of the cases. See the requirements part.

In the 10% left, it still make more sense to do the mapping manually than do try to make a library that has to do magic incantation to guess the user need.

Install

composer require mikesimonson/symfony-form-value-object-mapper

Usage

In any form that requires it you can use

$builder->setDataMapper(new FormMapper());

Requirement to use this Mapper:

  • Having entities
  • Having getters and setters on those entities
  • The name of the form elements must match the name of the properties in the entity
  • The name of the constructor parameters must match the name of the entities property
  • If yout throw an exception in case of validation failure it needs to extends InvalidArgumentException
  • Tests for your forms that use it. If you change a property name in your value object the form will break. (Although it's also true with the default form mapper from the symfony form)

Limitation:

Right now this mapper doesn't work with the collectionType field. PR are welcome.