Custom validator and message type specification

I have a special check in the enterprise check block. The method is DoValidateshown below.

protected override void DoValidate(Double objectToValidate, 
    object currentTarget, string key, ValidationResults validationResults)
{
    if (!IsSalMoreThanMinWage(objectToValidate))
    {
        //Here I need to mark this message as a "Warning"
        LogValidationResult(validationResults, 
            "Salary is too low for this state", currentTarget, key);
    }
}

I need to mark this verification failure as a warning message. In the interface, when I iterate over a collection ValidationResultsand grab an object ValidationResult, I will need to identify and group different types of messages and display them differently.

My question is: how can I mark a failure as a warning?

+3
source share
2 answers

Tag ValidationResult. , ValidationResults.

, :

<validator lowerBound="0" lowerBoundType="Inclusive" 
upperBound="255" upperBoundType="Inclusive" negated="false" messageTemplateResourceName="" messageTemplateResourceType="" 
messageTemplate="Oops a warning occurred" 
tag="Warning" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
name="My Validator" />


:

[StringLengthValidator(5, 50, Ruleset = "RuleSetA", Tag="Warning")]


, , Tag :

ValidationResults newResults = new ValidationResults();

foreach (ValidationResult vr in validationResults)
{
    newResults.AddResult( new ValidationResult( 
        vr.Message, vr.Target, vr.Key, "Warning", vr.Validator, vr.NestedValidationResults ) );
}


Tag ValidationResult, , :

foreach (ValidationResult vr in validationResults)
{
    if (string.Compare(vr.Tag, "Warning") == 0)
    {
        DisplayWarning(vr.Message);
    }
    else
    {
        DisplayError(vr.Message);
    }
}


, , ..

UPDATE

, - . , , , , , RuleSets.

RuleSets RuleSet . RuleSet, . - . , RuleSets:

  • RuleSet_Salary_Update
  • RuleSet_Salary_Update_Warning

, :

public static List<Validator<T>> CreateValidators<T>(bool shoulIncludeWarning, RuleSetType rulesetType)
{
     if (shouldIncludeWarning)
     {
         // Get warning validator if any
     }

     // Get Default validator (if any)
}

RuleSetType - (, , , , , PrimaryKey ..).

+2

:

"".

:

Application Validation " ". "". , .

, , , . , " , , !". , .

, ( ValidationException), ValidationResult. , , ( , ).

:

Button_Save(object sender, EventArgs e)
{
    this.Save(ValidationMode.Default);
}

Button_HellYesSuppresWarningsAndSave(object sender, EventArgs e)
{
    this.Save(ValidationMode.SuppressWarnings);
}


private Save(ValidationMode mode)
{
    try
    {
        ServiceLayer.Save(mode);
    }
    catch (ValidationException ex)
    {
        if (ex.ResultType == ValidationResultType.Warnings)
        {
            ShowWarningsAndAskIfSure(ex.Errors);
        }
        else
        {
            ShowErrorsAndTellUserNeedsToFix(ex.Errors);
        }
    }
}

- - :

public void Save(ValidationMode mode)
{
    Validate(ValidationResultType.Errors);

    if (!mode == ValidationMode.SuppressWarnings)
    {
        Validate(ValidationResultType.Warnings);
    }
}

private void Validate(ValidationResultType type)
{
    var objectToValidate;
    var ruleset = type == ValidationResultType.Errors ? "default" : "warnings";
    var validator = ValidationFactory
       .CreateValidator(objectToValidate.GetType(), ruleset);

    var results = validator.Validate();
    if (!results.IsValid)
    {
        throw new ValidationException(results, type);
    }
}

, .

+1

Source: https://habr.com/ru/post/1739034/


All Articles