guillaumeoriol / serquant

Serquant library
4 stars 1 forks source link

unbind filters and validators from the element names of the form to bind them with entity names #8

Closed guillaumeoriol closed 13 years ago

guillaumeoriol commented 13 years ago

In a simple form, element names match the property names of the corresponding entity (the business object). This simple design avoids having an additional mapper between external names (the ones used in the user interface) and internal names (of the domain). As property names of the entity are unique by design, there is no risk to get a collision between two form fields having the same id (a form represents a business object). No need to use the array notation in forms. Furthermore, it is forbidden to use this notation as Serquant\Service\Crud#populate (server side) and technema.grid.Editable#formToObject methods (client side) don't implement the required logic for it.

Simple entities (having no association) may be represented safely with this flat model. Unfortunately, more complex entities implementing associations will break this model. Let's think of a Customer entity having an association with an Address entity. Though internally customer details and address will belong to two separate entities, it will be displayed to the end user as a single form.

class Customer {
  private $id;
  private $name;
  private $address;
}

class Address {
  private $id;
  private $postalCode;
  private $city;
}

This implies a hierarchical representation of data that is incompatible with the flat model. From the domain point of view, outgoing data will be represented this way:

{
  "id": "1240",
  "name": "Dupont",
  "address": {
    "id": "9571",
    "postalCode": "77400",
    "city": "Lagny-sur-Marne"
  }
}

This is achieved by the entity serializer.

Unfortunately, the same data structure will be presented to the input filter for filtering and validation. As the form (holding the presentation, the filters and validators) and the entity have different structures, it doesn't work. We therefore need to de-couple form presentation from filtering/validating. This is achieved by removing every server-side filtering/validating code from the Zend_Form object and putting it in an Zend_Filter_Input object.