# Bug in Editor validation functions? Or expected behaviours?

**URL:** <https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652>\
**Category:** Developing Nodes\
**Created:** [6 November 2023 13:07 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652 "2023-11-06T13:07:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 November 2023 13:07 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652/1 "2023-11-06T13:07:26Z")

</div>

Hi all, refreshing my knowledge about processing nodes in the Editor. I seem to be getting some unexpected results when looking at how validation functions for fields in the edit panel are called. Not sure if this has always been this way or it is something new.

Validation fns are run:

1. x1 On every node instance when the Editor is loaded (e.g. panel not visible) - value is populated but jQ val is undefined.
2. x1 When a new node instance added - value & jq value both `undefined`.
3. x1 When the config panel is opened for an instance.
4. x\* When a field value has changed (on keypress) - value & jq value are the same.
5. x1 or x2? When a field value has changed (on blur) - value & jq value are the same.
6. x1 When field is changed and "Done" is clicked

Having it called on every keypress rather than on-blur seems a little excessive but I can understand it might occasionally be helpful.

However - It is also called when you **make changes to a different field**!

![Animation1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/126e7589c00c027150d585ef1afbbd7b4e13fa87.gif)

As you can see from the screen recording, though field validation is only set on the topic field, if you make changes to the name field it is still triggering a re-validation. This can't be right can it? It would surely be very inefficient, especially if the field validation needed to be intensive.

This is the script in the test node:

```auto
    /** Validate the topic input - this is called on Editor load, panel open, as well as on change
     * @param {*} value The input value to be validated
     * @returns {boolean} True if valid
     */
    function validateTopic(value) {
        // As this is called on Editor load, when panel opened AND when a change made
        // you may want to do things only when the panel is open (visible)
        if ($('#node-input-topic').is(':visible')) {
            console.log('#node-input-topic IS VISIBLE. ', `Changed? '${this.changed}'.`, `Value: '${value}'.`, `this.topic: '${this.topic}'.`, `jQ value: ${$('#node-input-topic').val()}.`)
        } else {
            console.log('#node-input-topic IS NOT VISIBLE. ', `Changed? '${this.changed}'.`, `Value: '${value}'.`, `this.topic: '${this.topic}'.`, `jQ value: ${$('#node-input-topic').val()}.`)
        }

        return true
    }

    /** Prep for edit
     * @param {*} node A node instance as seen from the Node-RED Editor
     */
    function onEditPrepare(node) {
        // log.log('onEditPrepare', node)

        tiTestbed.doTooltips('#ti-edit-panel') // Do this at the end
    } // ----- end of onEditPrepare() ----- //

    // @ts-ignore
    RED.nodes.registerType(moduleName, {
        // NOTE: Validations are called on Editor load, not just on change
        defaults: {
            name: { value: '' },
            topic: { value: '', validate: validateTopic },
        },
        align: 'left',
        inputs: 1,
        inputLabels: 'Some Input',
        outputs: 1,
        outputLabels: ['Output 1'],
        icon: 'font-awesome/fa-code',
        label: function () {
            return this.name || moduleName
        },
        category: tiTestbed.paletteCategory,
        color: 'var(--ti-testbed-node-colour)',
        paletteLabel: moduleName,

        /** Prepares the Editor panel */
        oneditprepare: function () { onEditPrepare(this) },
    }) // ---- End of registerType() ---- //

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [6 November 2023 13:28 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652/2 "2023-11-06T13:28:29Z")

</div>

There are cases where the validity of one field partially depends on the value of another field. Revalidating all entries is the safest way to ensure any errors are highlighted as expected.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 November 2023 13:51 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652/3 "2023-11-06T13:51:16Z")

</div>

OK, so not a bug 🙂 Thanks.

I'd just not noticed. I've needed to rework some validation on uibuilder nodes anyway so I'm just making sure I understand things correctly. I'd only just noticed that uibuilder makes a few too many API calls when validating things. Especially as the validation is also called on Editor page load. Not a problem for most people but my dev instance has dozens of instances which is a lot of calls.

And with this being the case, I may need to move some validation out to a jQuery change function instead.

Thanks again.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 November 2023 13:57 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652/4 "2023-11-06T13:57:30Z")

</div>

Just to clarify for anyone else who might find this useful.

Validation fns are run:

1. x1 On every node instance when the Editor is loaded (e.g. panel not visible) - value is populated but jQ val is undefined.
2. x1 When a new node instance added - value & jq value both `undefined`.
3. x1 When the config panel is opened for an instance.
4. x\* When ANY field value has changed (on keypress) - value & jq value are the same.
5. x1 or x2? When ANY field value has changed (on blur) - value & jq value are the same.
6. x1 or x2? When ANYTHING happens to ANY field (e.g. copy, paste, changing to a different window, ...)
7. x1 When "Done" is clicked even if nothing changed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 November 2023 13:58 UTC](https://discourse.nodered.org/t/bug-in-editor-validation-functions-or-expected-behaviours/82652/5 "2023-11-20T13:58:12Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
