我在node-red
中创建了一个自定义节点
<script type="text/javascript">
RED.nodes.registerType('project', {
category: 'My Category',
color: 'rgb(192, 237, 192)',
defaults: {
name: { value: "", required:true },
project: { value: "", required:true }
},
inputs: 0,
outputs: 1,
onpaletteadd: function (index) {
var node = this;
var sessionStorageData = sessionStorage.getItem(node.z);
if (sessionStorageData && (JSON.parse(sessionStorageData)).id != node.id) {
alert("Flow cannot have more then one Project node!!!")
}
if (!sessionStorageData) {
sessionStorageData = { id: node.id }
} else {
sessionStorageData = JSON.parse(sessionStorageData);
}
sessionStorageData.project = node.project;
sessionStorage.setItem(node.z, JSON.stringify(sessionStorageData));
},
oneditprepare: function (index) {
var node = this;
$.ajax({
type: "GET",
url: "../getExternalData?path=get",
dataType: "json",
success: function (data1) {
this.preload = true;
var appenddata1 = "";
$.each(data1, function (key, val) {
appenddata1 += "<option value = '" + key + "'>" + val + " </option>";
});
$("#node-input-project").append(appenddata1);
$("#node-input-project").val(node.project);
}
});
},
oneditsave: function (index) {
var node = this;
var sessionStorageData = sessionStorage.getItem(node.z);
if (!sessionStorageData) {
sessionStorageData = { id: node.id }
} else {
sessionStorageData = JSON.parse(sessionStorageData);
}
sessionStorageData.project = $("#node-input-project").val();
sessionStorage.setItem(node.z, JSON.stringify(sessionStorageData));
},
icon: "cog.png",
label: function () {
return this.name || "Project";
}
});
</script>
<script type="text/x-red" data-template-name="project">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i>Workflow Name</label>
<input type="text" id="node-input-name" placeholder="Workflow Name"/>
</div>
<div class="form-row">
<label for="node-input-project"><i class="icon-tag"></i> Project</label>
<select type="text" id="node-input-project">
<option value=" ">Please select a Project</option>
</select>
</div>
</script>
令我惊讶的是,required
仅使文本框变成红色,并且没有在单击Done
按钮时停止弹出窗口的关闭,我在return false
中尝试了oneditsave
,但是也没有帮助。
任何帮助将不胜感激。
答案 0 :(得分:1)
如创建节点的Node-RED docs中所述,您可以将validate
函数添加到节点html文件的defaults
部分。
有2个内置的验证器,
但是您也可以附加自己的功能:
defaults: {
minimumLength: { value:0, validate:RED.validators.number() },
lowerCaseOnly: {value:"", validate:RED.validators.regex(/[a-z]+/) },
custom: { value:"", validate:function(v) {
var minimumLength=$("#node-input-minimumLength").length?$("#node-input-minimumLength").val():this.minimumLength;
return v.length > minimumLength
} }
},
但是即使使用了这些或required: true
,用户仍然可以点击“完成”按钮。当用户尝试部署包含缺少字段值或无效字段值的节点的流时,将向用户显示警告,但无法阻止用户部署输入数据错误的流。