# Is it possible to dynamically change 'required' property for a dynamic property?

**URL:** <https://discourse.nodered.org/t/is-it-possible-to-dynamically-change-required-property-for-a-dynamic-property/10914>\
**Category:** Developing Nodes\
**Created:** [8 May 2019 09:26 UTC](https://discourse.nodered.org/t/is-it-possible-to-dynamically-change-required-property-for-a-dynamic-property/10914 "2019-05-08T09:26:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2019 09:26 UTC](https://discourse.nodered.org/t/is-it-possible-to-dynamically-change-required-property-for-a-dynamic-property/10914/1 "2019-05-08T09:26:18Z")

</div>

Is it possible to dynamically change 'required' property for a dynamic property?

What i mean is suppose you have 2 general purpose parameters on your node config editor that are sometimes required, sometimes not (depends on how other proprieties of the node are set)

```auto
        defaults: {
            name: { value: "" },
            parameter1: { value: "", required: *lookup_based_on_other_property*},
            parameter1Type: { value: "msg" },
            parameter2: { value: "", required: *lookup_based_on_other_property*},
            parameter2Type: { value: "msg" },

```

I have managed (kinda) by setting/resetting `node._def.defaults.parameter1.required` in `oneditsave` but when add a new node to the flow editor, the `required` property is set to the value set by the last node saved.

Additionally, are there any other events (like `onAddToFlowEditor`) available that may help in this regard?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2019 10:35 UTC](https://discourse.nodered.org/t/is-it-possible-to-dynamically-change-required-property-for-a-dynamic-property/10914/2 "2019-05-08T10:35:49Z")

</div>

Ok so I would ideally liked to have simply turned on/off the `required` property but i think i have a reasonable workaround in using `validate`...

```auto
    var myValidator = function(node,paramNo,v){
        var fn = node.selectedFunction;
        var parameterCount = fn && fn.parameters ? fn.parameters.length : 0;
        if(paramNo <= parameterCount){
            const p = fn.parameters[paramNo-1];
            if(p && p.required){
                return !!v;
            }
        }
        return true;
    }

    RED.nodes.registerType('myNode', {
        defaults: {
            name: { value: "" },
            parameter1: { value: "", validate: function(v){
                return myValidator(this,1,v) ;
            }},
            parameter1Type: { value: "msg" },
            parameter2: { value: "", validate: function(v){
                 return myValidator(this,2,v) ;
            }},
            ...
         }

```

However, although I get the orange triangle to tell me 1 or more parameters are invalid, I don't seem to get the typedInput marked as being invalid when I open the node editor?

---

<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:** [10 May 2019 13:00 UTC](https://discourse.nodered.org/t/is-it-possible-to-dynamically-change-required-property-for-a-dynamic-property/10914/3 "2019-05-10T13:00:59Z")

</div>

The validator function is the right way to do it. Not sure why you aren't getting the highlight though.

As a workaround, you can set it manually. Check another field to see what CSS class gets applied when the field is invalid and do that manually in the validator.

That's just a guess by the way, I think it should work.
