diff --git a/changelog.md b/changelog.md index 1dedaee..3e498f3 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ ### Updated - Redirect back with errors. This *may* be a breaking change if you have written any customizations to deal with issues surrounding `renderPageWithErrors` in `validate.helpers.js`. That method has been removed, and now on validation errors, we flash the errors to the session and redirect back to the `get` route. +- Moved duplicated nunjucks code from macros into `views/macros/sub-imports` ### Removed - `renderPageWithErrors` has been removed from `validate.helpers.js` diff --git a/views/base.njk b/views/base.njk index f408236..bea82f1 100644 --- a/views/base.njk +++ b/views/base.njk @@ -1,4 +1,3 @@ -{%- from 'validationMessage.njk' import validationMessage as validationMessage with context -%} {%- from 'input-text.njk' import textInput as textInput with context -%} {%- from 'input-textarea.njk' import textArea as textArea with context -%} {%- from 'radios.njk' import radioButtons as radioButtons with context -%} diff --git a/views/macros/checkboxes.njk b/views/macros/checkboxes.njk index 688c1ee..917f885 100644 --- a/views/macros/checkboxes.njk +++ b/views/macros/checkboxes.njk @@ -1,22 +1,16 @@ +{% from 'sub-imports/label-text.njk' import labelText with context %} +{% from 'sub-imports/hint-text.njk' import hintText with context %} +{% from 'sub-imports/validation-message.njk' import validationMessage with context %} + {% macro checkBoxes(key, values, selectedVals, question, errors, attributes) %}
- {% if attributes.required %} - - {% endif %} - {{ __(question) }} - {% if attributes.required %} - {{ __("required")}} - {% endif %} + {{ labelText(question, attributes.required) }} - {% if attributes.hint %} - {{ __(attributes.hint) }} - {% endif %} + {{ hintText(attributes.hint) }}
- {% if errors and errors[key] %} - {{ validationMessage(errors[key].msg, key) }} - {% endif %} + {{ validationMessage(errors, key)}} {% for index, val in values %}
diff --git a/views/macros/input-text.njk b/views/macros/input-text.njk index 70990f3..b6cbd42 100644 --- a/views/macros/input-text.njk +++ b/views/macros/input-text.njk @@ -1,3 +1,6 @@ +{% from 'sub-imports/label-text.njk' import labelText with context %} +{% from 'sub-imports/hint-text.njk' import hintText with context %} +{% from 'sub-imports/validation-message.njk' import validationMessage with context %} {# - `name`: field name - `label`: text for the label @@ -10,20 +13,10 @@ {% macro textInput(name, label, attributes) %}
- {% if attributes.hint %} - {{ __(attributes.hint) }} - {% endif %} - {% if errors and errors[name] %} - {{ validationMessage(errors[name].msg, name) }} - {% endif %} + {{ hintText(attributes.hint) }} + {{ validationMessage(errors, name) }}
{% endmacro %} \ No newline at end of file diff --git a/views/macros/input-textarea.njk b/views/macros/input-textarea.njk index 18f27fd..1443423 100644 --- a/views/macros/input-textarea.njk +++ b/views/macros/input-textarea.njk @@ -1,3 +1,7 @@ +{% from 'sub-imports/label-text.njk' import labelText with context %} +{% from 'sub-imports/hint-text.njk' import hintText with context %} +{% from 'sub-imports/validation-message.njk' import validationMessage with context %} + {# - `name`: field name - `label`: text for the label @@ -10,20 +14,10 @@ {% macro textArea(name, label, attributes) %}
- {% if attributes.hint %} - {{ __(attributes.hint) }} - {% endif %} - {% if errors and errors[name] %} - {{ validationMessage(errors[name].msg, name) }} - {% endif %} + {{ hintText(attributes.hint) }} + {{ validationMessage(errors, name) }}
{% endmacro %} \ No newline at end of file diff --git a/views/macros/radios.njk b/views/macros/radios.njk index 35b82a9..822e496 100644 --- a/views/macros/radios.njk +++ b/views/macros/radios.njk @@ -1,22 +1,16 @@ +{% from 'sub-imports/label-text.njk' import labelText with context %} +{% from 'sub-imports/hint-text.njk' import hintText with context %} +{% from 'sub-imports/validation-message.njk' import validationMessage with context %} + {% macro radioButtons(key, values, value, question, errors, attributes) %}
- {% if attributes.required %} - - {% endif %} - {{ __(question) }} - {% if attributes.required %} - {{ __("required")}} - {% endif %} + {{ labelText(question, attributes.required) }} - {% if attributes.hint %} - {{ __(attributes.hint) }} - {% endif %} + {{ hintText(attributes.hint) }}
- {% if errors and errors[key] %} - {{ validationMessage(errors[key].msg, key) }} - {% endif %} + {{ validationMessage(errors, key) }} {% for index, val in values %}
diff --git a/views/macros/sub-imports/hint-text.njk b/views/macros/sub-imports/hint-text.njk new file mode 100644 index 0000000..7a25e96 --- /dev/null +++ b/views/macros/sub-imports/hint-text.njk @@ -0,0 +1,5 @@ +{% macro hintText(text) %} + {% if text %} + {{ __(text) }} + {% endif %} +{% endmacro %} \ No newline at end of file diff --git a/views/macros/sub-imports/label-text.njk b/views/macros/sub-imports/label-text.njk new file mode 100644 index 0000000..c64d591 --- /dev/null +++ b/views/macros/sub-imports/label-text.njk @@ -0,0 +1,9 @@ +{% macro labelText(text, required) %} + {% if required %} + + {% endif %} + {{ __(text) }} + {% if required %} + {{ __("required")}} + {% endif %} +{% endmacro %} \ No newline at end of file diff --git a/views/macros/sub-imports/validation-message.njk b/views/macros/sub-imports/validation-message.njk new file mode 100644 index 0000000..0620eb9 --- /dev/null +++ b/views/macros/sub-imports/validation-message.njk @@ -0,0 +1,8 @@ +{% macro validationMessage(msg, id) %} + {% if errors and errors[id] %} + + {{ __('Error:') }} + {{ __(errors[id].msg) }} + + {% endif %} +{% endmacro %} \ No newline at end of file diff --git a/views/macros/validationMessage.njk b/views/macros/validationMessage.njk deleted file mode 100644 index 9bddaa6..0000000 --- a/views/macros/validationMessage.njk +++ /dev/null @@ -1,6 +0,0 @@ -{% macro validationMessage(msg, id) %} - - {{ __('Error:') }} - {{ __(msg) }} - -{% endmacro %} \ No newline at end of file