Search code examples
javascriptvalidationapostrophe-cms

Validating boolean field in apostrophe cms apostrophe-pieces-submit-widgets


I'm trying to build a simple contact form using apostrophe-pieces-submit-widgets and it needs a tick for GDPR. I set up a request-form module:

module.exports = {
  extend: 'apostrophe-pieces',
  name: 'request-form',
  label: 'Request Form',
  alias: 'requestForm',
  addFields: [
    ...
    {
      name: 'gdpr',
      label: 'Marketing agreement',
      type: 'boolean',
      required: true
    }
  ],
  //stuff like building title field and marking it as published when it's saved
  ...
}

And then I display it using apostrophe-pieces-submit-widgets:

module.exports = {
  extend: 'apostrophe-pieces-submit-widgets',
  fields: ['name', 'email', 'phone', 'gdpr']
}

It works visually, that is it is marked with an asterix, alas, you can still send the form without switching it to Yes. When you don't fill the other required fields, it's never mentioned in the returned error object as other fields are. What do I need to do to validate it?


Solution

  • You can now do this by setting the mandatory: true option for the field.