CKEditor Plugin Validation for Multiple Requirements

I’ve been working on a few CKEditor plugins recently for some projects. When building the elements of a dialog you create a data structure like:

elements: [{
    type: 'text',
    id: 'book',
    label: 'Book',
    validate: CKEDITOR.dialog.validate.notEmpty( "Book field cannot be empty" ),
}]

This is great, but what if you want multiple validation criteria for example it must be a number and not empty? There are a handful of hacks and examples of this online, but looking through the code I found an undocumented (and otherwise unused) function which enables you to combine multiple validation functions:

validate: CKEDITOR.dialog.validate.functions(
        CKEDITOR.dialog.validate.notEmpty(),
        CKEDITOR.dialog.validate.integer(),
        "(Start) chapter must be a number and is required",
    ),

You can pass as many validation functions into this as you want and it will combine them, but only allows you to specify one validation message unfortunately.

Leave a Reply

Your email address will not be published. Required fields are marked *