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

**URL:** https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397
**Category:** Developing Nodes
**Created:** [18 December 2021 16:14 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397 "2021-12-18T16:14:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [18 December 2021 16:14 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/1 "2021-12-18T16:14:57Z")

</div>

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.

```auto
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. 🤔

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.

---

<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: [18 December 2021 19:00 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/2 "2021-12-18T19:00:38Z")

</div>

That issue and resolution is explained in the docs.

[https://nodered.org/docs/creating-nodes/credentials#credentials-within-the-editor](https://nodered.org/docs/creating-nodes/credentials#credentials-within-the-editor)

---

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [19 December 2021 07:39 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/3 "2021-12-19T07:39:11Z")

</div>

Thank you so much for your advise. 😊

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'? 🤔

---

<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: [19 December 2021 11:24 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/4 "2021-12-19T11:24:19Z")

</div>

> [@studiobox](#):
>
> But if I want to validate when drag node into flow, are there any solution for varidation before checking 'oneditprepare'?

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

---

<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: [19 December 2021 13:31 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/5 "2021-12-19T13:31:30Z")

</div>

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

https://trello.com/c/oXRcDmhr.html

---

<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: [19 December 2021 18:26 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/6 "2021-12-19T18:26:54Z")

</div>

> [@studiobox](#):
>
> if I want to validate when drag node into flow, are there any solution for varidation before checking 'oneditprepare'

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:

```auto
    /** 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.

---

<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: [19 December 2021 19:25 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/7 "2021-12-19T19:25:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [21 December 2021 17:40 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/8 "2021-12-21T17:40:11Z")

</div>

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'. 😓

---

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [21 December 2021 17:54 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/9 "2021-12-21T17:54:33Z")

</div>

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?

---

<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: [21 December 2021 18:03 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/10 "2021-12-21T18:03:12Z")

</div>

> [@studiobox](#):
>
> Do you mean, "the credential property cannot have validate"?

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.

> [@studiobox](#):
>
> Do you have the example?

No. We have not implemented the feature yet.

---

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [21 December 2021 18:28 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/11 "2021-12-21T18:28:04Z")

</div>

> [@knolleary](#):
>
> We have not implemented the feature yet.

Ok. Thank you very much for your suggestion. 🙂

I will wait for this new feature.

---

<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: [21 December 2021 22:00 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/12 "2021-12-21T22:00:52Z")

</div>

> [@studiobox](#):
>
> jQuery for access input 'token', the result is 'undefined'

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.

---

<div class="post-metadata">

### Author: ![studiobox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/studiobox/32/55766_2.png) [@studiobox](https://discourse.nodered.org/u/studiobox)
#### Post date: [22 December 2021 12:35 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/13 "2021-12-22T12:35:16Z")

</div>

Hi,

Thank you for your suggestion.

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

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. 😅

---

<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 February 2022 12:36 UTC](https://discourse.nodered.org/t/how-can-i-raise-error-at-oneditsave-when-my-credential-is-empty-show-red-triangle-sign/55397/14 "2022-02-20T12:36:15Z")

</div>

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