Configure a Required Taxonomy Field
Introduction
Goal
Configure a required taxonomy field in a document type so that when saving a document of that type without any categories selected, the taxonomy field is visually marked as invalid using red coloring.
Background
A taxonomy field in a document type can be marked as required (meaning that at least one category must be selected) by adding the 'required' validator through configuration in the Console. When applying this to a default taxonomy field added using the Essentials setup application, attempting to save a document with no categories selected will cause a feedback message to be displayed only. The taxonomy field will not be highlighted in red as is usual for invalid fields in the document type editor. This page describes the additional configuration required to also highlight an invalid taxonomy field using red coloring.
Set a taxonomy field to be required
In order to mark a taxonomy field as required, the following steps have to be taken:
- Configure the org.onehippo.taxonomy.plugin.TaxonomyPickerPlugin frontend plugin.
- Set required validator on classifiable mixin or document type field.
1. Configure the TaxonomyPickerPlugin as frontend plugin
Although the standard way to configure a taxonomy field is to have a classifiable node with a org.hippoecm.frontend.editor.plugins.mixin.MixinLoaderPlugin below the editor:templates part of your document type, using this setup will only give a feedback message and will not show an invalidated field using red coloring.
To produce the correct validation highlighting, configure the TaxonomyPickerPlugin directly like in the example below:
/hippo:namespaces/myhippoproject/mydocumenttype/editor/templates/_default_: /classifiable: jcr:primaryType: frontend:plugin field: keys plugin.class: org.onehippo.taxonomy.plugin.TaxonomyPickerPlugin taxonomy.classification.dao: service.taxonomy.dao taxonomy.id: service.taxonomy taxonomy.name: exampletaxonomy wicket.id: ${cluster.id}.field (or applicable layout part e.g. ${cluster.id}.right.item)
2. Set 'required' validator on classifiable mixin or document type field
Now, we need to set a required validator on the taxonomy field. We can do this on the mixin level or on the document type level.
2a. Set classifiable mixin as 'required'
Setting the mixin hippotaxonomy:classifiable as required will make the taxonomy fields in every document type required. To do that, add a validator required here:
/hippo:namespaces/hippotaxonomy/classifiable/hipposysedit:nodetype/hipposysedit:nodetype: /keys: jcr:primaryType: hipposysedit:field hipposysedit:validators: [ required ]
2b. Add a field with validator 'required' to the document type
For setting the taxonomy field as required on document type level, add a field node below the hipposysedit:nodetype part of the type, also with a validator required.
/hippo:namespaces/myhippoproject/mydocumenttype/hipposysedit:nodetype/hipposysedit:nodetype: /keys: jcr:primaryType: hipposysedit:field hipposysedit:multiple: true hipposysedit:path: hippotaxonomy:keys hipposysedit:type: String hipposysedit:validators: [ required ]
The result
The screenshot below, taken using the demo project, shows what a required taxonomy field will look in the CMS: