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)
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?
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
...
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?
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.