Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a default error summary presenter #311

Merged
merged 6 commits into from
Aug 31, 2021

Conversation

peteryates
Copy link
Member

The intention of this presenter is that it provides the current basic behaviour but also provides a convenient way for people with specific requirements to inject them.

It currently requires that it's initialized with an error_messages hash but we may yet tweak that so an instance can be passed in and the error messages set on it.

Refs #310

The intention of this presenter is that it provides the current basic
behaviour but also provides a convenient way for people with specific
requirements to inject them.

It currently requires that it's initialized with an error_messages hash
but we may yet tweak that so an instance can be passed in and the error
messages set on it.

Refs #310
@peteryates peteryates added the enhancement New feature or request label Aug 28, 2021
@peteryates
Copy link
Member Author

@jsugarman - having had a bit of a think about it, I think this approach will give you what you need. If we always use a presenter it may as well handle the collection of error messages rather than one-at-a-time.

If you need more info, we could always optionally pass the object to the initializer too, something like:

def presenter
  @presenter.new(error_messages, @builder.object)
end

Alternatively, we could pass an instance of the presenter to #govuk_error_summary. Happy with any of the approaches.

@jsugarman
Copy link
Collaborator

Many thanks @peteryates. I just saw this when I pushed up my new branch directly against this repo.

I will take a better look when I have time but my initial thoughts is that I don't think this approach will work for our custom error presenter without a major, and rather painful, rewrite on our end. We instantiate our presenter with the model object and a translation file (for lookups) and then have methods to retrieve the summary and field level errors. We pass custom attributes/keys which are then converted to ordinalize nested instances (e.g. attribute : defendant_2_first_name, message blank would become "Enter a first name for the second defendant").

I had added some thoughts to this commit while working.

However I am not even sure the approach I have taken in my PR would work for us going forward and applying this functionality to other models because I can see that we generate separate line item summary errors for the same attribute with multiple errors.

I will take a better look at your PR when I can but do let me know any thoughts, problems, improvements on mine when you can.

This approach offers a little more flexibility when than just accepting
a class. When a class is provided it's instantiated with the object's
error messages but when an object is supplied it's used as-is.

It needs to implement the method #formatted_error_messages so we check
for that, but any other attributes and state can be set up in the
application and can contain whatever's necessary.
@peteryates
Copy link
Member Author

@jsugarman - thanks for the feedback, I kind of had this in mind - I've updated it so it'll accept any object in addition to a presenter class.

I believe this will work with your PR now with minimal changes, I think you just need to add a #formatted_error_messages to your presenter class and have it loop through the object's error attributes yourself. I'll take a closer look when I get chance.

@jsugarman
Copy link
Collaborator

jsugarman commented Aug 29, 2021

I just pulled your branch and had a quick play tested against an implementation of the formatted_error_messages in our presenter and it works 🎉 .

I will have a further look but it I like it's simplicity

It makes sense to allow a app-wide default to be set and gives us an
opportunity to document and test the process better.
Copy link
Collaborator

@jsugarman jsugarman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇 Have this working as I'd hope for against our app branch.

Couple of minor documentation comments plus do you think this is worth its own entry in the guide to provide an interactive example. You could cherry pick and amend this commit, or am happy to add myself.

Co-authored-by: Joel Sugarman <joel.sugarman@digital.justice.gov.uk>
jsugarman and others added 2 commits August 31, 2021 14:29
To clarify its purpose and usage I have extended the
existing error summary documentation template, examples
and helpers.

I could extend further to add a nested fields type example
if deemed worthwhile.
@peteryates peteryates marked this pull request as ready for review August 31, 2021 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants