Validation of Min/Max Value in csla 4.x

Validation of Min/Max Value in csla 4.x

Old forum URL: forums.lhotka.net/forums/t/9997.aspx


tsvideo posted on Friday, January 28, 2011

Hello!

 I want to Validate 2 Fields MinValue and MaxValue, that MaxValue is more or equal to MinValue.

 My first Solution was to make one Validation Object, but this doesn't Work because the Data Portal throw an exception that there ist already a Element with the same name.

 My Code (try) with one Validation Object:

  /// <summary>

  /// ValidateMinMaxString Prüfen, ob MaxWert größer oder gleich MinWert ist (Umwandlung in Zahl)

  /// </summary>

  public class ValidateMinMaxString : Csla.Rules.BusinessRule

  {

    private Csla.Core.IPropertyInfo _minProperty;

    private Csla.Core.IPropertyInfo _maxProperty;

     public ValidateMinMaxString(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo minProperty, Csla.Core.IPropertyInfo maxProperty)

      : base(primaryProperty)

    {

      _minProperty = minProperty;

      _maxProperty = maxProperty;

      InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty };

      InputProperties.Add(minProperty);

      InputProperties.Add(maxProperty);

    }

     protected override void Execute(Csla.Rules.RuleContext context)

    {

      int minValue;

      int maxValue;

       if (int.TryParse((string)context.InputPropertyValues[_minProperty], out minValue)

        & int.TryParse((string)context.InputPropertyValues[_maxProperty], out maxValue))

        if (minValue > maxValue)

          context.AddErrorResult("Der Max Wert muß größer oder gleich dem Min Wert sein");

    }

  }

 Protected void AddBusinessRules()

{

      BusinessRules.AddRule(new ValidateMinMaxString(MinwertProperty, MinwertProperty, MaxwertProperty));
BusinessRules.AddRule(new ValidateMinMaxString(MaxwertProperty, MinwertProperty, MaxwertProperty));

      BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(MinwertProperty, MaxwertProperty));

}

 My Working Solution, with two Validation Objects:

  /// <summary>

  /// ValidateMinMaxString Prüfen, ob MaxWert größer oder gleich MinWert ist (Umwandlung in Zahl)

  /// </summary>

  public class ValidateMinString : Csla.Rules.BusinessRule

  {

    private Csla.Core.IPropertyInfo _maxProperty;


    public ValidateMinString(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo maxProperty)

      : base(primaryProperty)

    {

      _maxProperty = maxProperty;

      InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty, maxProperty };

    }

    protected override void Execute(Csla.Rules.RuleContext context)

    {

      int minValue;

      int maxValue;

      if (int.TryParse((string)context.InputPropertyValues[PrimaryProperty], out minValue)

        & int.TryParse((string)context.InputPropertyValues[_maxProperty], out maxValue))

        if (minValue > maxValue)

          context.AddErrorResult("Der Min Wert muß kleiner oder gleich dem Max Wert sein");

    }

  }

  /// <summary>
  /// ValidateMinMaxString Prüfen, ob MaxWert größer oder gleich MinWert ist (Umwandlung in Zahl)
  /// </summary>
  public class ValidateMaxString : Csla.Rules.BusinessRule

  {

    private Csla.Core.IPropertyInfo _minProperty;

    public ValidateMaxString(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo minProperty)

      : base(primaryProperty)

    {

      _minProperty = minProperty;

      InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty, minProperty };

    }

    protected override void Execute(Csla.Rules.RuleContext context)

    {

      int minValue;

      int maxValue;

      if (int.TryParse((string)context.InputPropertyValues[PrimaryProperty], out maxValue)

        & int.TryParse((string)context.InputPropertyValues[_minProperty], out minValue))

        if (minValue > maxValue)

          context.AddErrorResult("Der Max Wert muß größer oder gleich dem Min Wert sein");

    }

  }

 protected void AddBusinessRules()

    {

      BusinessRules.AddRule(new ValidateMinString(MinwertProperty, MaxwertProperty));

      BusinessRules.AddRule(new ValidateMaxString(MaxwertProperty, MinwertProperty));

      BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(MinwertProperty, MaxwertProperty));

 BusinessRules.AddRule(new Csla.Rules.CommonRules.Dependency(MaxwertProperty, MinwertProperty));
}

My Question:

Is there a Way to have only one Validation Object ?

Thank you for help

 

JonnyBee replied on Friday, January 28, 2011

yes, you can. This rule already exists in CslaContrib: http://cslacontrib.codeplex.com. Look at the LessThan and GreaterThan rules that compare 2 fields.

JonnyBee replied on Friday, January 28, 2011

yes, you can. I would however prefer to have separate rules. As separate rules these already exists in CslaContrib: http://cslacontrib.codeplex.com. Look at the LessThan and GreaterThan rules that compare 2 fields.

tsvideo replied on Friday, January 28, 2011

Hello Jonny,

thank you for your answer, i would look in CslaContrib.

Thilo

Copyright (c) Marimer LLC