Datatable editor node.js - .dateFormat() error: key.toLowerCase is not a function

Datatable editor node.js - .dateFormat() error: key.toLowerCase is not a function

CapamaniaCapamania Posts: 233Questions: 81Answers: 5

I'm using datatable editor with node.js. When trying to validate serverside a date with the internal .dateFormat() function like this:

const moment = require('moment');

...

new Field( 'messages_access.valid_until' )
    .validator( Validate.dateFormat(
        "YYYY-MM-DD",
        new Validate.Options({
            message: "Please provide a valid date: YYYY-MM-DD"
        })
    ))

I get this error message:

TypeError: key.toLowerCase is not a function
    at normalizeLocale (/home/project/node_modules/moment/moment.js:2041:26)
    at chooseLocale (/home/project/node_modules/moment/moment.js:2055:21)
    at getLocale (/home/project/node_modules/moment/moment.js:2251:16)
    at prepareConfig (/home/project/node_modules/moment/moment.js:2942:44)
    at createFromConfig (/home/project/node_modules/moment/moment.js:2928:44)
    at createLocalOrUTC (/home/project/node_modules/moment/moment.js:3022:16)
    at createLocal (/home/project/node_modules/moment/moment.js:3026:16)
    at hooks (/home/project/node_modules/moment/moment.js:16:29)
    at /home/project/node_modules/datatables.net-editor-server/dist/validators.js:624:25
    at step (/home/project/node_modules/datatables.net-editor-server/dist/validators.js:32:23)
    at Object.next (/home/project/node_modules/datatables.net-editor-server/dist/validators.js:13:53)
    at /home/project/node_modules/datatables.net-editor-server/dist/validators.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/home/project/node_modules/datatables.net-editor-server/dist/validators.js:3:12)
    at /home/project/node_modules/datatables.net-editor-server/dist/validators.js:614:20
    at Field.<anonymous> (/home/project/node_modules/datatables.net-editor-server/dist/field.js:363:46)            

How to solve this?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Could you use:

    new Field( 'messages_access.valid_until' )
        .validator( Validate.dateFormat(
            "YYYY-MM-DD",
            null,
            new Validate.Options({
                message: "Please provide a valid date: YYYY-MM-DD"
            })
        ))
    

    please? The second parameter of the dateFormat function is the locale to give to MomentJS. We can pass null to have it use its default.

    Allan

This discussion has been closed.