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

Allow custom sort order for error summaries #290

Merged
merged 8 commits into from
Jun 14, 2021

Conversation

peteryates
Copy link
Member

@peteryates peteryates commented Jun 12, 2021

Ordering can be defined in one of two ways:

  • passing in an order argument, to #govuk_error_summary, e.g., order: %i(name email date_of_birth)

  • configuring an default_error_summary_error_order_method method, like this:

    GOVUKDesignSystemFormBuilder.configure do |conf|
       conf.default_error_summary_error_order_method = :error_order
    end

    Then the error order can be defined on a per-model basis like this:

    def error_order
      %i(name email date_of_birth)
    end

The method approach has the benefit of being automatically applied on any bound form automatically. The argument-based approach takes precedence and allows for some extra flexibility, but requires a little extra attention and possibly repetition.

Ordered attributes will have their error messages displayed first, in the order defined.

Any errors on attributes that aren't covered by #error_order will appear after those that are, in the order they were defined in the object or model.

If no custom ordering is provided the current default behaviour will apply and the errors will be displayed in the same order they're defined in the model.

@peteryates peteryates added the enhancement New feature or request label Jun 12, 2021
Currently this depends on there being a #error_order method being
present on the object/model associated with the builder. I went for this
approach so that with objects that are represented by several forms
through an app, there's no duplication in the views. It may be worth
reconsidering this, but it feels broadly sensible.

The way ordering works is the #error_order method should return an array
of attribute names, eg: [:name, :email, :age] and when building the list
of errors the error summary promotes these to the start.

Any errors on attributes that aren't covered by #error_order will appear
after those that are, in the order they were defined in the object or
model.

If there is no #error_order method on the object, don't try to re-order
and just go with what Rails gives us, which is the order they were
defined.
@peteryates peteryates force-pushed the custom-sort-order-for-error-summary branch from c0e74ca to 019f9eb Compare June 12, 2021 20:07
This is important because teams might have different preferences or may
already have a method called error_order on their model, which would
lead to unexpected behaviour.
It makes more sense for this functionality to be opt-in rather than on
by default.
@peteryates peteryates merged commit d052c0e into master Jun 14, 2021
@peteryates peteryates deleted the custom-sort-order-for-error-summary branch June 14, 2021 08:24
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.

1 participant