Closed timarney closed 4 years ago
TLDR; Going to stick to user defined mark as required fields for this PR.
For the future: Moving forward we can look at doing this based on the form schemas.
Given a form schemas we don't have a quick way of saying whether a field is required or not
i.e. is required true or false
We have checks such as
isLength,
custom checks,
isEmail,
isIn,
conditional required fields etc...
As far as making the data available we can make things like res locals available to the macros which in turn could look up a required field.
Sample code:
// app.js
app.set('view engine', 'njk')
app.set('engine', env)
const routeMiddleware = (route, locale) => (req, res, next) => {
// ...more code
const engine = res.app.get('engine')
engine.addGlobal('locals', res.locals)
return next()
}
{% macro textInput(name, label, attributes) %}
...
<code>{{ locals.route.name}} !!!</code
// do the lookup based on the field name + making the schema available here
See: https://blog.eexit.net/nunjucks-inject-express-request/
Thinking we would write some functions to handle i.e. isRequiredField.
That said the scope stretches quite a bit beyond what I think we need in this PR. Needs some further thinking.
Adds * and (required) to field labels when you pass
As an attribute to the macro.
https://github.com/cds-snc/node-starter-app/issues/98
Note: Ideally I would like to hook this to the form schema i.e. one place to set required on the fields
@JuliannaR if you can take a look at this that would be great.
Example: