Devexpress DxValidateProvidor validate

本文关键字:validate DxValidateProvidor Devexpress | 更新日期: 2023-09-27 18:15:32

我想检查DevExpress控件中输入的值是否为空,但我的验证总是返回false。因此,我无法执行进一步的操作。

如果你能帮忙,我会很感激的。

 public static class ValidateHelpers {
        public static bool validate(Form f, params Control[] control) {
            bool result = true;
            if (control != null) {
                KgsDxValidationProvider prov = new KgsDxValidationProvider { ValidationMode = ValidationMode.Auto };
                ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule {
                    ConditionOperator = ConditionOperator.IsNotBlank,
                    ErrorText = "You Must Enter A Value",
                    ErrorType = ErrorType.Critical
                };
                foreach (var item in control) {
                    prov.SetValidationRule(item, notEmptyValidationRule);
                    result = false;
                }
                f.ValidateChildren();
            }
            return result;
        }
}

Devexpress DxValidateProvidor validate

您不需要验证表单上的所有子元素。这将做你想做的:

private bool Validate(params Control[] controls)
{
   bool result = controls == null || !controls.Any();
   if (controls != null)
   {
       DXValidationProvider provider = new DXValidationProvider { ValidationMode = ValidationMode.Auto };
       ConditionValidationRule noEmptyValues = new ConditionValidationRule
       {
           ConditionOperator = ConditionOperator.IsNotBlank,
           ErrorText = @"You must enter a value",
           ErrorType = ErrorType.Critical
       };
       foreach (Control control in controls)
       {
          provider.SetValidationRule(control, noEmptyValues);
       }
       result = provider.Validate(); //Validate all controls associated with the provider
    }
   return result;
 }

如果您需要自定义验证等方面的进一步帮助,您可以查看此链接