在 LightSwitch 2011 的屏幕端添加客户验证消息

发布于 2024-12-09 21:47:47 字数 242 浏览 0 评论 0原文

我有一个带有 CreateNewUser Screen 的小型灯开关应用程序。

屏幕字段来自数据源中添加的数据表,我已在数据源端对字段进行了所有验证 不过,我在屏幕中添加了一个自定义控件,名为:重新输入密码。

每当用户单击“保存”按钮时,我想匹配密码和重新输入密码,并希望在密码和重新输入密码不同时提示用户验证消息。

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

谢谢

I have small lightswitch application having CreateNewUser Screen.

Screen fields comes from Datatable added in DataSource, I have made all Validattion of fields at DataSource side
However i have added one Custom Control in the screen named : Re-TypePassword.

Whenever user click on save button I want to match the Password and Re-TypePassword and want to prompt user a validation message if Password and Re-TypePassword are different.

how can i display that validation massage ? ( I mean i want to add validation message to Summary of messages that appears at the top of screen)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-12-16 21:47:47

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

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

One way is to add the -re type password- field to the database, then you can use the usual validation.

Not the best way, but it's a way :)

相守太难 2024-12-16 21:47:47

条件验证............ http://forums.asp.net/t/1924941.aspx?Conditional+Validation+using+DataAnnotation

[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;
    }
}

Conditional validation............... http://forums.asp.net/t/1924941.aspx?Conditional+Validation+using+DataAnnotation

[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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文