开发者

How to get Custom validation error?

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)
    开发者_JAVA百科    {           

            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?


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();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜