Dynamically set required property

/me again, probably with another simple question.

The node I'm designing has a checkbox ("autoActive") and a configuration field ("configAutomatic"). No science.

defaults: {
    name: {},
    autoActive: {},
    configAutomatic: {type: "shading automatic", required: false}
}

...

<div class="form-row">
    <label><i class="fa fa-power-off"></i> Enable</label>
    <input type="checkbox" id="node-input-autoActive" style="display:inline-block; width:auto;">
</div>
<div class="form-row">
    <label for="node-input-configAutomatic"><i class="fa fa-sliders"></i> Configuration</label>
    <input type="text" id="node-input-configAutomatic">
</div>

As you see, configAutomatic required is set to false. Do you know a way to set this dynamically? Means, true if checkbox active, false if not?

Hugs! Tom

Hi @danube

No, there is currently no way to dynamically alter the required state of a node property.

It might be possible to fake by getting rid of the required flag and add a custom validate function to the field that does the work to decide if it should be set or not. That does get a little involved as the function will be called both when the node is added to the workspace (so must operate only using the node properties) as well as when editing the node (so would have to check for the existence of the edit form and use the current checkbox state rather than node property).

Ah, that gem should really be in the docs for custom nodes :slight_smile:

Great, thanks for letting me know (in warpspeed!).

Just for the records, as I was looking quite a time for one solution:

defaults: {
    name: {},
    autoActive: {},
    configAutomatic: {type: "shading automatic", validate: function() {return this.configAutomatic != "" || !this.autoActive}}
}

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