Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Mark fields as required #107

Merged
merged 10 commits into from
Oct 23, 2019
Merged

Mark fields as required #107

merged 10 commits into from
Oct 23, 2019

Conversation

timarney
Copy link
Member

@timarney timarney commented Oct 17, 2019

Adds * and (required) to field labels when you pass

{ required: true }

As an attribute to the macro.

#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:

Screen Shot 2019-10-17 at 11 40 59 AM

@timarney timarney temporarily deployed to cds-node-starter-pr-107 October 17, 2019 15:39 Inactive
@timarney
Copy link
Member Author

timarney commented Oct 17, 2019

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.

@timarney timarney changed the title WIP - Required fields Mark fields as required Oct 17, 2019
@timarney timarney temporarily deployed to cds-node-starter-pr-107 October 17, 2019 16:54 Inactive
@timarney timarney temporarily deployed to cds-node-starter-pr-107 October 17, 2019 16:59 Inactive
@timarney timarney merged commit ebcb8be into master Oct 23, 2019
@timarney timarney deleted the required-fields branch October 23, 2019 16:00
@timarney timarney mentioned this pull request Oct 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants