以下代码适用于文本框,我可以使用自己的函数覆盖默认验证程序函数并强制它FilteringSelect
,使输入无效。但是,对于var customValidator = function (value) {
console.log("custom validator: ", value);
return false; //force an invalid result
};
formelement.theInput.set('validator', customValidator);
var res = formelement.theInput.validate();
console.log("res: ", res);
输入,这根本不起作用。以下代码始终返回true - 实际上它永远不会进入我的自定义验证函数。
这意味着它正在执行与预期不同的验证器,或者我在错误的输入上设置验证器(渲染时有多个验证器)。
<div data-dojo-attach-point="formelement" >
<select data-dojo-type="dijit/form/FilteringSelect" data-dojo-attach-point="theInput"
data-dojo-props="labelAttr:'text', searchAttr:'text', required:false, " >
</select>
</div>
小部件模板看起来像这个
<div data-dojo-attach-point="formelement" style="position: absolute; left: 222px; top: 75px; width: 200px; border-width: 1px; border-style: none; border-color: black;" class="fe-dropdown resize-handle" id="4180a54b-3931-472e-8c5f-212bfddc88b2" widgetid="uniqName_10_10">
<div class="dijit dijitReset dijitInline dijitLeft fe-element dijitTextBox dijitComboBox dijitValidationTextBox dijitTextBoxFocused dijitComboBoxFocused dijitValidationTextBoxFocused dijitFocused" id="widget_dijit_form_FilteringSelect_28" role="combobox" aria-haspopup="true" data-dojo-attach-point="_popupStateNode" widgetid="dijit_form_FilteringSelect_28" aria-disabled="false" popupactive="true" aria-expanded="false" aria-owns="dijit_form_FilteringSelect_28_popup">
<div class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" data-dojo-attach-point="_buttonNode" role="presentation">
<input class="dijitReset dijitInputField dijitArrowButtonInner" value="▼ " type="text" tabindex="-1" readonly="readonly" role="button presentation" aria-hidden="true">
</div>
<div class="dijitReset dijitValidationContainer">
<input class="dijitReset dijitInputField dijitValidationIcon dijitValidationInner" value="Χ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</div>
<div class="dijitReset dijitInputField dijitInputContainer">
<input class="dijitReset dijitInputInner" type="text" autocomplete="off" data-dojo-attach-point="textbox,focusNode" role="textbox" aria-autocomplete="both" tabindex="0" id="dijit_form_FilteringSelect_28" aria-required="false" value="" aria-invalid="false" maxlength="25" style="font-family: Arial; font-weight: 400; font-style: normal; color: black; text-decoration: none; text-align: left; background-color: transparent;">
<input type="hidden" value="CA">
</div>
</div>
</div>
渲染时看起来像这样
var query = from
fish in ColorsArray.Select((t, i) => new { fishName = t, fishLength = LengthArray[i]})
group fish by fish.fishName
into team
select new
{
fishName = team.Key,
maxLength = team.Max(item => item.fishLength)
};
// get max length of fish by name
var maxLength = query.FirstOrDefault(fish => fish.fishName == "Blue")?.maxLength;
答案 0 :(得分:2)
我认为由于isValid
函数,它在FilteringSelect中被覆盖,因此它不会与textBox具有相同的行为。
我建议在将自定义验证器设置为以下后再次使用isValid:
formelement.theInput.isValid =
function() {
return this.validator(this.textbox.value,this.get('constraints'));
// force new validator above
}
Bellow是一个示例代码段:
require(["dijit/registry", "dijit/form/FilteringSelect","dojo/on","dijit/form/Button","dojo/ready"],
function(registry,dom,On,Button,ready){
ready(function(){
var customValidator = function (value) {
console.log("custom validator: ", value);
return false; //force an invalid result
};
formelement = registry.byId("formelement");
formelement.set('validator', customValidator);
formelement.isValid = function() {
return this.validator(this.textbox.value, this.get('constraints'));}
var res = formelement.validate();
console.log("res: ", res);
var btn = registry.byId("btn");
On(btn,"click",function(){
console.log(formelement.validate());
});
})
});
<script>
dojoConfig = {
isDebug: true,
parseOnLoad: true,
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet"/>
<body class="claro">
<div data-dojo-type="dijit/form/Button" id="btn"> Validate </div>
<div data-dojo-attach-point="formelement" >
<select id="formelement" data-dojo-type="dijit/form/FilteringSelect" data-dojo-attach-point="theInput"
data-dojo-props="labelAttr:'text', searchAttr:'text', required:false, " >
</select>
</div>
</body>