验证撇号cms中的布尔字段 apostrophe-pieces-submit-widgets

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

我正在尝试使用 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。当您不填写其他必填字段时,它永远不会像其他字段那样在返回的错误对象中提及。我需要做什么来验证它?

除非我误解了,撇号 验证布尔值(允许为 No / false),它只是不是您要允许的值通过。 boolean 字段确实不适合这种情况。

我无法想象您如何使用现有的架构字段实现此目标..

您可以按照这组教程创建自己的架构字段(可以根据需要验证)。 https://apostrophecms.org/docs/tutorials/intermediate/custom-schema-field-types.html

colorpicker 示例有点复杂,通过 Apostrophe 源代码查看并在新名称下复制布尔字段类型,并强制值为 truthy 以验证可能更容易。您需要的一切都在 apostrophe-schemas 模块中。

您现在可以通过为该字段设置 mandatory: true 选项来执行此操作。