C# 在LightSwitch 2011的屏幕侧添加Custome验证消息

C# 在LightSwitch 2011的屏幕侧添加Custome验证消息,c#,.net,vb.net,visual-studio-lightswitch,C#,.net,Vb.net,Visual Studio Lightswitch,我有一个具有CreateNewUser屏幕的小型lightswitch应用程序 屏幕字段来自数据源中添加的Datatable,我已经在数据源端对字段进行了所有验证 但是,我在屏幕中添加了一个名为:Re-TypePassword的自定义控件 每当用户点击保存按钮时,我想匹配密码和重新键入密码,如果密码和重新键入密码不同,我想提示用户一条验证消息 如何显示该验证信息?(我的意思是我想将验证消息添加到屏幕顶部显示的消息摘要中) 谢谢一种方法是将-re-type password-字段添加到数据库中,然

我有一个具有CreateNewUser屏幕的小型lightswitch应用程序

屏幕字段来自数据源中添加的Datatable,我已经在数据源端对字段进行了所有验证 但是,我在屏幕中添加了一个名为:Re-TypePassword的自定义控件

每当用户点击保存按钮时,我想匹配密码和重新键入密码,如果密码和重新键入密码不同,我想提示用户一条验证消息

如何显示该验证信息?(我的意思是我想将验证消息添加到屏幕顶部显示的消息摘要中)


谢谢

一种方法是将-re-type password-字段添加到数据库中,然后可以使用通常的验证


不是最好的方法,但它是一种方法:)

条件验证

[RequiredIf(“isSelected”,true)]
公共类requireditAttribute:ConditionalValidationAttribute
{
受保护的重写字符串验证名称
{
获取{return“requiredif”;}
}
public requirediftAttribute(字符串相关属性,对象targetValue)
:base(新的RequiredAttribute(),dependentProperty,targetValue)
{
}
受保护的重写IDictionary GetExtraValidationParameters()
{
返回新词典
{ 
{“规则”,“必需”}
};
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple=false)]
公共抽象类ConditionalValidationAttribute:ValidationAttribute,IClientValidatable
{
受保护的只读ValidationAttribute InnerAttribute;
公共字符串依赖属性{get;set;}
公共对象TargetValue{get;set;}
受保护的抽象字符串验证名称{get;}
受保护的虚拟IDictionary GetExtraValidationParameters()
{
返回新字典();
}
受保护的条件ValidationAttribute(ValidationAttribute innerAttribute、string dependentProperty、object targetValue)
{
this.InnerAttribute=InnerAttribute;
this.DependentProperty=DependentProperty;
this.TargetValue=TargetValue;
}
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
//获取对此验证所依赖的属性的引用
var containerType=validationContext.ObjectInstance.GetType();
var field=containerType.GetProperty(this.DependentProperty);
如果(字段!=null)
{
//获取依赖属性的值
var dependentvalue=field.GetValue(validationContext.ObjectInstance,null);
//将该值与目标值进行比较
如果((dependentvalue==null&&this.TargetValue==null)| |(dependentvalue!=null&&dependentvalue.Equals(this.TargetValue)))
{
//match=>表示我们应该尝试验证此字段
如果(!InnerAttribute.IsValid(值))
{
//验证失败-返回错误
返回新的ValidationResult(this.ErrorMessage,new[]{validationContext.MemberName});
}
}
}
返回ValidationResult.Success;
}
公共IEnumerable GetClientValidationRules(ModelMetadata元数据、ControllerContext上下文)
{
var rule=new ModelClientValidationRule()
{
ErrorMessage=FormatErrorMessage(metadata.GetDisplayName()),
ValidationType=ValidationName,
};
string depProp=BuildDependentPropertyId(元数据,上下文为ViewContext);
//找到我们所依赖的控件上的值;如果它是bool,则将其格式化为javascript样式
字符串targetValue=(this.targetValue???).ToString();
if(this.TargetValue.GetType()==typeof(bool))
{
targetValue=targetValue.ToLower();
}
rule.ValidationParameters.Add(“dependentproperty”,depProp);
规则.ValidationParameters.Add(“targetvalue”,targetvalue);
//添加额外的参数(如果有)
foreach(GetExtraValidationParameters()中的var param)
{
rule.ValidationParameters.Add(param);
}
收益率-收益率规则;
}
私有字符串BuildDependentPropertyId(ModelMetadata元数据,ViewContext)
{
string depProp=viewContext.ViewData.TemplateInfo.GetFullHtmlFieldDid(this.DependentProperty);
//这将在开头追加当前字段的名称,因为TemplateInfo的上下文已将此字段名称追加到它。
var thisField=metadata.PropertyName+“\uux”;
if(depProp.StartsWith(此字段))
{
depProp=depProp.Substring(thisField.Length);
}
返回depProp;
}
}
[RequiredIf("isSelected", true)]
public class RequiredIfAttribute : ConditionalValidationAttribute
{
    protected override string ValidationName
    {
        get { return "requiredif"; }
    }
    public RequiredIfAttribute(string dependentProperty, object targetValue)
        : base(new RequiredAttribute(), dependentProperty, targetValue)
    {
    }
    protected override IDictionary<string, object> GetExtraValidationParameters()
    {
        return new Dictionary<string, object> 
    { 
        { "rule", "required" }
    };
    }
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public abstract class ConditionalValidationAttribute : ValidationAttribute, IClientValidatable
{
    protected readonly ValidationAttribute InnerAttribute;
    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }
    protected abstract string ValidationName { get; }

    protected virtual IDictionary<string, object> GetExtraValidationParameters()
    {
        return new Dictionary<string, object>();
    }

    protected ConditionalValidationAttribute(ValidationAttribute innerAttribute, string dependentProperty, object targetValue)
    {
        this.InnerAttribute = innerAttribute;
        this.DependentProperty = dependentProperty;
        this.TargetValue = targetValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // get a reference to the property this validation depends upon
        var containerType = validationContext.ObjectInstance.GetType();
        var field = containerType.GetProperty(this.DependentProperty);
        if (field != null)
        {
            // get the value of the dependent property
            var dependentvalue = field.GetValue(validationContext.ObjectInstance, null);

            // compare the value against the target value
            if ((dependentvalue == null && this.TargetValue == null) || (dependentvalue != null && dependentvalue.Equals(this.TargetValue)))
            {
                // match => means we should try validating this field
                if (!InnerAttribute.IsValid(value))
                {
                    // validation failed - return an error
                    return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
                }
            }
        }
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule()
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = ValidationName,
        };
        string depProp = BuildDependentPropertyId(metadata, context as ViewContext);
        // find the value on the control we depend on; if it's a bool, format it javascript style
        string targetValue = (this.TargetValue ?? "").ToString();
        if (this.TargetValue.GetType() == typeof(bool))
        {
            targetValue = targetValue.ToLower();
        }
        rule.ValidationParameters.Add("dependentproperty", depProp);
        rule.ValidationParameters.Add("targetvalue", targetValue);
        // Add the extra params, if any
        foreach (var param in GetExtraValidationParameters())
        {
            rule.ValidationParameters.Add(param);
        }
        yield return rule;
    }

    private string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
    {
        string depProp = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(this.DependentProperty);
        // This will have the name of the current field appended to the beginning, because the TemplateInfo's context has had this fieldname appended to it.
        var thisField = metadata.PropertyName + "_";
        if (depProp.StartsWith(thisField))
        {
            depProp = depProp.Substring(thisField.Length);
        }
        return depProp;
    }
}