Reputation: 2155
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?
Upvotes: 0
Views: 158
Reputation: 7572
You can now do this by setting the mandatory: true
option for the field.
Upvotes: 1
Reputation: 2157
Unless I'm misunderstanding, Apostrophe is validating a boolean (which is allowed to be No / false
), it's just not the value you want to allow through. A boolean
field really doesn't cut it for this case.
I can't think how you'd achieve this with the existing schema fields..
You can create your own schema fields (which can validate out however you like) by following this set of tutorials. https://apostrophecms.org/docs/tutorials/intermediate/custom-schema-field-types.html
The colorpicker example is a bit complex, it might be easier to look through Apostrophe source and duplicate the boolean fieldtype under a new name, and force the value to be truthy in order to validate. Everything you need is in the apostrophe-schemas
module.
Upvotes: 0