weixin_39530557
weixin_39530557
2020-12-27 09:00

CTL-90: ModelBase Add Remove ValidationResults

Jira issue originally created by user facos:

thanks for making ValidationContext.Add and Remove ValidationResults public but i'm missing the correct notifications when adding or removing them and the events are not accessable.

how about these additions in ModelBase? Thanks a lot!

    protected void NotifyValidationResult(IValidationResult validationResult, bool notifyGlobal)
    {
        Argument.IsNotNull("validationResult", validationResult);

        var propertyName = string.Empty;

        if (validationResult is IFieldValidationResult)
        {
            propertyName = ((IFieldValidationResult)validationResult).PropertyName;
        }

        switch (validationResult.ValidationResultType)
        {
            case ValidationResultType.Warning:
                this.NotifyWarningsChanged(propertyName, notifyGlobal);
                break;

            case ValidationResultType.Error:
                this.NotifyErrorsChanged(propertyName, notifyGlobal);
                break;
        }
    }

    public void AddBusinessRuleValidationResult(IBusinessRuleValidationResult businessRuleValidationResult)
    {
        Argument.IsNotNull("businessRuleValidationResult", businessRuleValidationResult);

        var validationContext = this.ValidationContext;

        if (validationContext != null)
        {
            validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult);
            this.NotifyValidationResult(businessRuleValidationResult, true);
        }
    }

    public void AddFieldValidationResult(IFieldValidationResult fieldValidationResult)
    {
        Argument.IsNotNull("fieldValidationResult", fieldValidationResult);

        var validationContext = this.ValidationContext;

        if (validationContext != null)
        {
            validationContext.AddFieldValidationResult(fieldValidationResult);
            this.NotifyValidationResult(fieldValidationResult, true);
        }
    }

    public void RemoveBusinessRuleValidationResult(IBusinessRuleValidationResult businessRuleValidationResult)
    {
        Argument.IsNotNull("businessRuleValidationResult", businessRuleValidationResult);

        var validationContext = this.ValidationContext;

        if (validationContext != null)
        {
            validationContext.RemoveBusinessRuleValidationResult(businessRuleValidationResult);
            this.NotifyValidationResult(businessRuleValidationResult, true);
        }
    }

    public void RemoveFieldValidationResult(IFieldValidationResult fieldValidationResult)
    {
        Argument.IsNotNull("fieldValidationResult", fieldValidationResult);

        var validationContext = this.ValidationContext;

        if (validationContext != null)
        {
            validationContext.RemoveFieldValidationResult(fieldValidationResult);
            this.NotifyValidationResult(fieldValidationResult, true);
        }
    }

该提问来源于开源项目:Catel/Catel

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39530557 weixin_39530557 4月前

    Comment created by :

    In the next validation sequence, they will be added to the list. Just make sure to call validate at the end.

    点赞 评论 复制链接分享
  • weixin_39530557 weixin_39530557 4月前

    Comment created by :

    I can understand the NotifyValidationResult and have added it with docs. However, why do you need the other ones?

    点赞 评论 复制链接分享
  • weixin_39530557 weixin_39530557 4月前

    Comment created by facos:

    Thanks a lot for adding NotifyValidationResult. This will be a great help. The other functions are just there for convenience (add and remove validation results from the validation context with notifications) and to reduce repeating code.

    点赞 评论 复制链接分享
  • weixin_39530557 weixin_39530557 4月前

    Comment created by :

    Ok, then please add them to your own code base. We are trying to get the ModelBase smaller, not extending it. Maybe extension methods or something like that.

    点赞 评论 复制链接分享
  • weixin_39530557 weixin_39530557 4月前

    Issue was closed with resolution "Fixed"

    点赞 评论 复制链接分享
  • weixin_39812065 weixin_39812065 4月前

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

    点赞 评论 复制链接分享

相关推荐