Javascript KendoUI,敲除,字段的条件验证
我将kendoui小部件与knockoutjs一起用于数据源。我有一个复选框,它是绑定到Javascript KendoUI,敲除,字段的条件验证,javascript,html,knockout.js,kendo-ui,Javascript,Html,Knockout.js,Kendo Ui,我将kendoui小部件与knockoutjs一起用于数据源。我有一个复选框,它是绑定到StartClientFromWebEnabled可观察变量的数据。仅当选中复选框ic时,输入文本框才可见(StartClientFromWebEnabled为true)。输入具有必需的属性。我希望只在选中复选框时触发所需的验证 这是我的html: <table> <tr> <td><label for="startClientFromWebE
StartClientFromWebEnabled
可观察变量的数据。仅当选中复选框ic时,输入文本框才可见(StartClientFromWebEnabled
为true)。输入具有必需的属性。我希望只在选中复选框时触发所需的验证
这是我的html:
<table>
<tr>
<td><label for="startClientFromWebEnabled">Client Launch From Web:</label></td>
<td><input type="checkbox" id="startClientFromWebEnabled" name="startClientFromWebEnabled" data-bind="checked: StartClientFromWebEnabled, enable: IsEditable" onchange="startClientFromWebToggleRequiredAttribute()" /></td>
</tr>
<tr data-bind="visible: StartClientFromWebEnabled">
<td><label for="mimeType">Protocol:</label></td>
<td>
<input id="mimeType" name="mimeType" data-bind= "value: MimeType, enable: IsEditable" />
<span class="k-invalid-msg" data-for="mimeType"></span>
</td>
</tr>
</table>
问题是,对于我的应用程序中的许多依赖属性,我需要此功能,我的选项是使用一些参数使此函数成为泛型函数,并使用相应的paramater值从html调用它,如下所示:
toggleRequiredAttribute = function (checkboxElement, inputElement1, inputElement2 ... ) {
var checkbox = document.getElementById(checkboxElement);
var inputElement1 = document.getElementById(inputElement1);
if (checkbox.checked) {
inputElement1.setAttribute("required", "required");
}
else {
inputElement1.removeAttribute("required");
}
}
<input type="checkbox" id="startClientFromWebEnabled" name="startClientFromWebEnabled" data-bind="checked: StartClientFromWebEnabled, enable: IsEditable" onchange="toggleRequiredAttribute('startClientFromWebEnable', 'mimeType')" />
toggleRequiredAttribute=函数(CheckBox元素、inputElement1、inputElement2…){
var checkbox=document.getElementById(checkboxElement);
var inputElement1=document.getElementById(inputElement1);
如果(复选框。选中){
inputElement1.setAttribute(“必需”、“必需”);
}
否则{
输入元素1.删除属性(“必需”);
}
}
我真的不喜欢这种情况。我想知道kendoui中是否有类似条件验证的东西,只有在满足某些条件时才会触发。欢迎提出任何其他建议 我也遇到了同样的问题,我创建了一个自定义验证器,该验证器还处理服务器端验证,这个示例不是100%完成,但所有验证都在工作,它根据复选框状态验证字符串长度,它还使用错误消息等资源,因此需要稍加修改,它使用kendo ui验证客户端,请告诉我这是否有用: 模型属性:
public bool ValidateTextField { get; set; }
[CustomValidator("ValidateTextField", 6, ErrorMessageResourceType=typeof(Errors),ErrorMessageResourceName="STRINGLENGTH_ERROR")]
public string TextField{ get; set; }
自定义验证程序:
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
public class CustomValidatorAttribute : ValidationAttribute, IClientValidatable {
private const string defaultErrorMessage="Error here.";
private string otherProperty;
private int min;
public CustomValidatorAttribute(string otherProperty, int min) : base(defaultErrorMessage) {
if(string.IsNullOrEmpty(otherProperty)) {
throw new ArgumentNullException("otherProperty");
}
this.otherProperty=otherProperty;
this.min=min;
this.ErrorMessage = MyResources.Errors.STRINGLENGTH_ERROR;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
bool valid = true;
var curProperty = validationContext.ObjectInstance.GetType().
GetProperty(otherProperty);
var curPropertyValue = curProperty.GetValue
(validationContext.ObjectInstance, null);
if(Convert.ToBoolean(curPropertyValue)) {
string str=value.ToString();
valid = str.Length >= min;
if(!valid) { return new ValidationResult(MyResources.Errors.STRINGLENGTH_ERROR); }
}
return ValidationResult.Success;
}
#region IClientValidatable Members
public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
var rule=new ModelClientValidationRule {
ErrorMessage = this.ErrorMessage,
ValidationType="checkboxdependantvalidator"
};
rule.ValidationParameters["checkboxid"]=otherProperty;
rule.ValidationParameters["min"]=min;
yield return rule;
}
public override string FormatErrorMessage(string name) {
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
name);
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
公共类CustomValidatorAttribute:ValidationAttribute,IClientValidatable{
private const string defaultErrorMessage=“此处出错。”;
私有财产;
私用int min;
公共CustomValidatorAttribute(字符串otherProperty,int-min):基(defaultErrorMessage){
if(string.IsNullOrEmpty(otherProperty)){
抛出新ArgumentNullException(“otherProperty”);
}
this.otherProperty=otherProperty;
这个.min=min;
this.ErrorMessage=MyResources.Errors.STRINGLENGTH\u错误;
}
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext){
bool valid=true;
var curProperty=validationContext.ObjectInstance.GetType()。
GetProperty(其他属性);
var curPropertyValue=curProperty.GetValue
(validationContext.ObjectInstance,null);
if(Convert.ToBoolean(curPropertyValue)){
字符串str=value.ToString();
有效=str.Length>=min;
if(!valid){返回新的ValidationResult(MyResources.Errors.STRINGLENGTH_ERROR);}
}
返回ValidationResult.Success;
}
#区域IClientValidable成员
public System.Collections.Generic.IEnumerable GetClientValidationRules(ModelMetadata元数据,ControllerContext上下文){
var规则=新ModelClientValidationRule{
ErrorMessage=this.ErrorMessage,
ValidationType=“checkboxDependentValidator”
};
rule.ValidationParameters[“checkboxid”]=otherProperty;
rule.ValidationParameters[“min”]=min;
收益率-收益率规则;
}
公共重写字符串FormatErrorMessage(字符串名称){
返回String.Format(CultureInfo.CurrentUICulture,ErrorMessageString,
姓名);
}
}
Javascript:
(function ($, kendo) {
$.extend(true, kendo.ui.validator, {
rules: { // custom rules
customtextvalidator: function (input, params) {
//check for the rule attribute
if (input.filter("[data-val-checkboxdependantvalidator]").length) {
//get serialized params
var checkBox = "#" + input.data("val-checkboxdependantvalidator-checkboxid");
var min = input.data("val-checkboxdependantvalidator-min");
var val = input.val();
if ($(checkBox).is(':checked')) {
if (val.length < min) {
return false;
}
}
}
return true;
}
},
messages: { //custom rules messages
customtextvalidator: function (input) {
// return the message text
return input.attr("data-val-checkboxdependantvalidator");
}
}
});
})(jQuery, kendo);
(函数($,剑道){
$.extend(true,kendo.ui.validator{
规则:{//自定义规则
customtextvalidator:函数(输入,参数){
//检查规则属性
if(input.filter(“[data val CheckBoxDependent Validator]”)长度){
//获取序列化参数
var checkBox=“#”+input.data(“val checkboxDependent Validator checkboxid”);
var min=输入数据(“val CheckboxDependent Validator min”);
var val=input.val();
如果($(复选框).is(':checked')){
如果(值长度<最小值){
返回false;
}
}
}
返回true;
}
},
消息:{//自定义规则消息
customtextvalidator:函数(输入){
//返回消息文本
返回input.attr(“数据值校验器”);
}
}
});
})(jQuery,剑道);
有用的帖子:
我只对客户端验证感兴趣,我对使用Fluent验证的服务器端验证没有问题。我不太明白代码中的javascript部分与我的案例有什么关系。你能用我发布的示例代码(仅javascript部分)给出一个示例吗?对于你的需求来说,这可能有些过分,但是服务器端代码将属性添加到javascript使用的复选框和文本框中。当剑道验证程序启动时,它首先获取文本框的“val checkboxDependent validator checkboxid”属性,使用该属性获取复选框的状态,然后在需要时验证文本框。
(function ($, kendo) {
$.extend(true, kendo.ui.validator, {
rules: { // custom rules
customtextvalidator: function (input, params) {
//check for the rule attribute
if (input.filter("[data-val-checkboxdependantvalidator]").length) {
//get serialized params
var checkBox = "#" + input.data("val-checkboxdependantvalidator-checkboxid");
var min = input.data("val-checkboxdependantvalidator-min");
var val = input.val();
if ($(checkBox).is(':checked')) {
if (val.length < min) {
return false;
}
}
}
return true;
}
},
messages: { //custom rules messages
customtextvalidator: function (input) {
// return the message text
return input.attr("data-val-checkboxdependantvalidator");
}
}
});
})(jQuery, kendo);