我正在尝试使用apostrophe-pieces-submit-widgets
构建一个简单的联系表单,它需要为GDPR打勾。我设置了一个请求表单模块:
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
...
}
然后我用apostrophe-pieces-submit-widgets
显示它:
module.exports = {
extend: 'apostrophe-pieces-submit-widgets',
fields: ['name', 'email', 'phone', 'gdpr']
}
它在视觉上有效,也就是用星号标记,唉,你仍然可以发送表格而不用切换到Yes
。如果不填写其他必填字段,则返回的错误对象中从未提及过其他字段。我需要做些什么来验证它?
您现在可以通过为字段设置mandatory: true
选项来完成此操作。
除非我误解,Apostrophe正在验证一个布尔值(允许为No / false
),它只是你想要通过的值。在这种情况下,boolean
领域确实没有削减它。
我无法想象你如何使用现有的架构字段实现这一目标。
您可以按照这组教程创建自己的架构字段(可以随意验证)。 https://apostrophecms.org/docs/tutorials/intermediate/custom-schema-field-types.html
colorpicker示例有点复杂,可能更容易查看Apostrophe源并以新名称复制boolean fieldtype,并强制该值为truthy以便验证。你需要的一切都在apostrophe-schemas
模块中。