Finishing the web application with Sequent

In the previous guide Building a web application
we created a web application using Sequent with Sinatra. In this guide we will continue
with that web application and will show you how to add Form validation, and let the Author add Posts.

Adding form validation

Every web application needs some sort of form validation. When creating a Sequent application
you typically bind a Command to a web form. A Command respresents
user intent, like AddPost or AddAuthor. Sequent does not provide any view helpers to render
errors in the UI like for instance Rails does. Sequent does provide a way to do Command validation
using the Validation module from Rails. Please check validations
in our Reference Guide for all the details. For now we stick to the ‘create author’ form in our
web application.

If we fire up the blog application and open the home page and
directly hit the ‘Create author’ button the form blows up with a

The only checks we do it that the name and email should be present, but
since Sequent uses the Rails validation module you can add any validates method
available.

So in order to provide proper feedback to the user we need to handle this
error in Sinatra and display the error messages at the correct fields.

For this guide we somewhat refactored the web application and added bootstrap
for some nifty look and feel and extracted some command erb code into
a default layout, this is automatically picked up by Sinatra.

Added the rescue block and stored the errors in @errors. We will
use this in the erb to display the error messages.

We have changed the command attribute into an instance variable @command
so it is available in the erb for displaying the value in the form fields.
This is necessary when submitting the form with an error. If this happens
you want to show the values the user typed in.

Now we need to change the html form. First we add some code to ensure the
form fields are rendered in red when it contains an invalid value:

When we now submit an empty form the error messages are displayed
underneath the input fields.

So to summarize, when creating a web application:

In Sequent you typically bind forms to Command objects

You can rescue from the Sequent::Core::CommandNotValid in order
to display validation errors in the Commands

Using Sinatra it is trivial to display those errors the ‘Railsy’ way.

What about errors that occur outside Command validation? Remember that
we enforce uniqueness of email addresses in the Usernames AggregateRoot.
This will raise a Usernames::UsernameAlreadyRegistered error and is
not rescued in our web application.

Again this is not something that Sequent handles for you since it is not
a web framework. It is however not that hard to rescue from. Since we only
have one custom error class in this example we will rescue this error explictly.

Tip: If your application grows you can of course create a base error class
for your app and rescue from that in you Sinatra controllers.

Since we follow a naming convention in the AddPost command and EditPost command
we can use the same form.

Wrap up

In this guide we have added form validation and added the possibility
for Authors to add and edit Posts.
If in your domain it is also necessary for an Author
keeps track of it’s posts to enforce some sort of business rule
then you will also need to add this to your domain logic.
This can be done for instance in the PostCommandHandler: