如何获得自定义验证错误?

发布于 2024-09-28 11:45:20 字数 813 浏览 2 评论 0原文

假设我有一个具有 typeid 和 salary 属性的实体 Person。我为这两个属性构建了一个业务规则,例如:

public static partial class MyRules
    {
        public static ValidationResult Rule1(Person p, ValidationContext context)
        {           

            if ((p.typeid == 1) && ((p.salary == null))
            {
                return new ValidationResult("type 1 must should have salary",
                                            new string[] { "Salary" });
            }

            return ValidationResult.Success;
        }
    }

代码通过 share.cs 放置在服务器端。

因此,当违反规则时,我将 SubmitOperation.HasError = true;这种错误仅在调用 SubmitChanges 后出现。并且错误不会显示在 ValidationSummary 中

,因此当 SubmitOperation.HasError = true;我如何知道 SubmitOperation 错误是验证错误而不是其他错误?当我可以将此错误识别为验证错误时,如何获取验证错误消息“类型 1 必须有薪水”并将其显示给用户?

Suppose I have entity Person with properties typeid and salary. I build a business rule for this two properties like:

public static partial class MyRules
    {
        public static ValidationResult Rule1(Person p, ValidationContext context)
        {           

            if ((p.typeid == 1) && ((p.salary == null))
            {
                return new ValidationResult("type 1 must should have salary",
                                            new string[] { "Salary" });
            }

            return ValidationResult.Success;
        }
    }

The code is put at server side with share.cs.

So when the rule is violated, I wil have SubmitOperation.HasError = true; This kind of error only after call SubmitChanges. and the error does not display in ValidationSummary

So when SubmitOperation.HasError = true; how can I know SubmitOperation error is validation error not other error? When I can identify this error as validation error, how can I get the validation error message "type 1 must should have salary" and show it to user?

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

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

发布评论

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

评论(1

不忘初心 2024-10-05 11:45:20

我知道这已经快一年了;然而,没有人回答这个问题。我还没有回答任何问题,但我知道答案(或至少 1 个可能的答案)(请(并感谢)标记为已回答)。以下是我在提交操作完成时处理验证结果的方式。调用 SubmitChanges 时,您需要使用回调和用户状态来调用重载。这可能有点令人困惑,因为在回调 (InsertEntityCompleted) 中我将 op.userstate 称为回调,但这就是在此实例中使用用户状态的方式。

private void InsertEntityCompleted( SubmitOperation op )
    {
        var callback = op.UserState as Action<Exception, ICollection<ValidationResult>>;
        if ( callback != null )
        {
            var validationResults = new Collection<ValidationResult>();
            if ( op.HasError )
            {
                foreach ( var entity in op.EntitiesInError )
                {
                    //HOW YOU KNOW SUBMIT OPERATION ERROR IS VALIDATION ERROR
                    if ( entity.HasValidationErrors )
                        foreach ( var validationResult in entity.ValidationErrors )
                        {
                            //HOW YOU WOULD GET THE ERROR MESSAGES AND MEMBER NAMES
                            var name = validationResult.MemberNames;
                            var error = validationResult.ErrorMessage;
                            validationResults.Add(validationResult);
                        }

                }
                op.MarkErrorAsHandled();
            }
            //HOW YOU IDENTIFY ERROR AS VALIDATION ERROR AND NOT OTHER TYPE OF ERROR
            if ( op.Error != null &&
                op.Error is DomainOperationException &&
                ( op.Error as DomainOperationException ).Status == OperationErrorStatus.ValidationFailed )
                //I CALLBACK NULL FOR EXCEPTIONS, BUT I KEEP THE VALIDATION RESULTS ON VALIDATION ERRORS
                //THEN TO SHOW IT TO THE USER I IMPLEMENT INotifyDataErrorInfo IN MY VIEWMODEL 
                //AND MANAGE THE ERRORS THROUGH THAT IMPLEMENTATION, THE BOUND CONTROL SHOULD HAVE 
                //NotifyOnValidationError=True DEFINED IN THE BINDING
                callback( null, validationResults );
            else
                callback( op.Error, validationResults );
            _entityContext.Enitities.EntityContainer.Clear();
        }
    }

I know this is almost a year old; nonetheless, no one has answered it. And I have not answered any questions yet but I know the answer, (or at least 1 possible answer) (please (and thanks) mark as answered). Here is how I handle validation results on submit operations when they are completed. When calling SubmitChanges you need to call the overload with a callback and a user state. It might be a bit confusing because in the callback (InsertEntityCompleted) I call the op.userstate a callback, but thats how the user state is used in this instance.

private void InsertEntityCompleted( SubmitOperation op )
    {
        var callback = op.UserState as Action<Exception, ICollection<ValidationResult>>;
        if ( callback != null )
        {
            var validationResults = new Collection<ValidationResult>();
            if ( op.HasError )
            {
                foreach ( var entity in op.EntitiesInError )
                {
                    //HOW YOU KNOW SUBMIT OPERATION ERROR IS VALIDATION ERROR
                    if ( entity.HasValidationErrors )
                        foreach ( var validationResult in entity.ValidationErrors )
                        {
                            //HOW YOU WOULD GET THE ERROR MESSAGES AND MEMBER NAMES
                            var name = validationResult.MemberNames;
                            var error = validationResult.ErrorMessage;
                            validationResults.Add(validationResult);
                        }

                }
                op.MarkErrorAsHandled();
            }
            //HOW YOU IDENTIFY ERROR AS VALIDATION ERROR AND NOT OTHER TYPE OF ERROR
            if ( op.Error != null &&
                op.Error is DomainOperationException &&
                ( op.Error as DomainOperationException ).Status == OperationErrorStatus.ValidationFailed )
                //I CALLBACK NULL FOR EXCEPTIONS, BUT I KEEP THE VALIDATION RESULTS ON VALIDATION ERRORS
                //THEN TO SHOW IT TO THE USER I IMPLEMENT INotifyDataErrorInfo IN MY VIEWMODEL 
                //AND MANAGE THE ERRORS THROUGH THAT IMPLEMENTATION, THE BOUND CONTROL SHOULD HAVE 
                //NotifyOnValidationError=True DEFINED IN THE BINDING
                callback( null, validationResults );
            else
                callback( op.Error, validationResults );
            _entityContext.Enitities.EntityContainer.Clear();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文