Processing input from web forms using an Unpresenter

on August 15, 2014 Technical and Tags: , with 0 comments

Often when presenting information on the web the format for storage and the format used for display and storage are different. A very common example of this is handling dates where the display is typically configurable based on some form of locale settings but dates are stored in UTC in a database dependent way.

Every time you need to display the information it is necessary to convert to a display format. These conversions can lead to error prone and repetitive code in your applications.

In many Laravel based web applications this problem is solved by using a “presenter” where the presenter class wraps the model class and performs the necessary conversions for display. This is good programming practice since it separates the concerns of format conversion from that data transportation/storage.

Presenters are not a complex class and a number of packages exist to perform this task, some more automatic that others. For a good explanation see Phillip Brown’s Culttt article – Model Presenters in Laravel 4, or Jeffrey Way on Laracasts – View Presenters from Scratch.

Processing input

An inverse challenge also exists when processing input from web forms or through an API. In this case you need to be converting information from an entered format into a format for storage. Again this is repetitive code – what is the best way to solve this challenge?

Enter the Unpresenter

In a number of projects SauveSolutions have solved this problem by implementing a variant of a Presenter, which we have called an “Unpresenter.” Like a Presenter, an Unpresenter class simply takes the input and transforms it into a format suitable for storage. It is doing the opposite of a Presenter.

We have also found that the Unpresenter is a great place to wrap up input validation as well. As a best practice we recommend validating all user inputs especially for type and length. We only use this for basic input validation.

The unpresenter is once again an implementation of the Decorator pattern. In this case it is decorating the the input data array instead of an Eloquent model instance.

Due to the frequency we have encountered the problems we have built date conversion into the base Unpresenter class. We have also built in automatic handling of checkbox fields. Checkboxes were causing a lot of repetitive code since the field is only present in the request body if the box is checked.

Handling checkboxes avoids this type of defensive coding

if (isset($input['checkbox_field']) {
$model->checked = true;
} else {
$model->checked=false;
}

Wrapping this approach up in an Unpresenter provides a set of known inputs regardless of the type of the specific input fields.

The Unpresenter also has the option to define mutator functions for any other transformations you may require. If a mutator is defined then it will be called to return the transformed value.

Package

The package has been uploaded to Github, sauvesolutions/presenters, and is available on composer, simply add sauvesolutions/presenters to your require section in composer.json. The current version is 0.2.0.

Laravel 4.3

It looks like some of the functionality around Validation in the above will be overtaken by the new approach in Laravel 4.3. The automatic injection of the FormRequest into the controller methods is a great improvement. It looks like much of the functionality in the Unpresenter could also be merged into the FormRequest class as well – but at this time it requires more investigation.