How can I raise error at 'oneditsave' when my credential is empty? (show red triangle sign)

Hi,

I have some problem about 'credentials' validation.

At first, I add credential in my custom node for keep my secret token. I added 'require' and 'validate' properties. See below.

credentials: {
            token: {
                type: "text",
                required: true,
                validate: function(v) {
                    return v.length > 0;
                }
            }
        },

But when I use my custom node at runtime. If I leave the secret token input empty, and then 'Done' from edit, there is not something for error notification. For example, display a red triangle on my customer node to indicate that no input values have been entered. :thinking:

So, How can I raise error at 'oneditsave' when my credential is empty?
Such as, show red triangle sign or something for notification to user, There are some error in my custom node. Please suggest me.

That issue and resolution is explained in the docs.

https://nodered.org/docs/creating-nodes/credentials#credentials-within-the-editor

Thank you so much for your advise. :blush:

Ok. Now I can validate when 'oneditprepare'. But if I want to validate when drag node into flow, are there any solution for varidation before checking 'oneditprepare'? :thinking:

Yes. But I need some time to explain. Bit busy till later, I'll come back to you.

I've added an item to the backlog to support the 'required' flag with credentials - although it's easier said than done.

There is a way to do things when dropping a node onto your flow and even before the first deploy for that node.

Here is an example from the next version of uibuilder:

    /** Track which urls have been used - required to handle copy/paste and import
     *  as these can contain duplicate urls before deployment.
     */
    RED.events.on('nodes:add', function(node) {
        if ( node.type === 'uibuilder') {
            // Keep a list of uib nodes in the editor
            // may be different to the deployed list
            editorInstances[node.id] = node.url
            // -- IF uibuilderInstances <> editorInstances THEN there are undeployed instances. --
        }
    })

That code goes in the javascript script in your html file. It goes outside RED.nodes.registerType and it is set up maybe even when a node is added to the palette, can't quite remember. Anyway, put some console log statements in so that you can see when it is triggered. Obviously, the RED.events.on('nodes:add', function(node) {}) function is triggered when ANY node is added so make sure you include a test to limit to just the node type you are interested in.

Whilst that does give you hook into the event of when a node is added to the workspace, it doesn't let you do any custom validation of the node - there's no way to mark a node as invalid so it displays an error. We don't provide any way to do custom validation beyond the individual validate functions on the properties - and you can't have those functions on credential type properties.

I've already linked to the backlog item to let you to mark a credential as required (which doesn't require knowledge of its actual value).

We already have another backlog item to let a node provide a custom validate function that isn't tied to an individual property.

Hi,

Thank you very much for your reply again.
I try to use
RED.events.on('nodes:add', function(node) {})
outside registerType. It can use console.log("Test") for displaying "Test" text.
But when I use jQuery for access input 'token', the result is 'undefined'. :sweat:

Hi,

Thank you very much for your reply.
Do you mean, "the credential property cannot have validate"?
And about

We already have another backlog item to let a node provide a custom validate function that isn't tied to an individual property.

Do you have the example?

With Node-RED today, you cannot do any validation of credentials.

We have items on the backlog to improve that situation in a future release.

No. We have not implemented the feature yet.

Ok. Thank you very much for your suggestion. :slightly_smiling_face:

I will wait for this new feature.

You can't use jQuery there because the context is different at that point. The config panel for your node does not exist when that event fires. The node exists so you can certainly check it's current settings values but the config panel does not exist - so it doesn't make sense to check if any values have been changed by the user in the UI at that point because they can't be.

I use it to make sure that none of the URL's across multiple instances of uibuilder are duplicates reguardless - if you were to add 10 instances of uibuilder and not deploy between adding each, you could end up with multiples of the same URL which would cause problems.

Hi,

Thank you for your suggestion.

Now I can use temporary solution by using input 'checkbox' for help. :blush:

I set default value 'false' for this checkbox'.
And I set validate function for this checkbox is return its value directly.

Then, at 'oneditsave' event,
I add algorithm for checking input 'token', which it has value or not?
If input token has value, then I use jQuery for set checkbox 'check' -> then its value is true.
But if input token has not value, then I use jQuery for set checkbox 'check' -> then its value is false.

I hope, this solution does not have the error. :sweat_smile: