DynamicRangeAttribute

namespace: MvcControlsToolkit.Core.DataAnnotations

The DynamicRangeAttribute is a property level validation attribute. It declares a property value be less than a fixed maximum, and/or greated than a fixed minimum, and/or greater than all values in a list of other properties, and/or less than the values in a list of other properties. Dynamic minimums and maximums may have constant tolerances added/subtracted to them.

The MvcControlsToolkit.Core package contains adapters to perform validation also on the client side.

Two RequiredFieldsAttribute with different settings may be applied to the same property, only if one set just fixed maximum/minimum and the other set just dynamic minima/maxima. This may be usefull to provide different error messages for the two validations.

It works, basically, with all ordered data types: all numbers, DateTime, TimeSpan, Week, and Month types. The type must be specified in the constructor. Important: Since it accepts mix of T/Nullable<T>, please always specify the base type (T) also for nullable types. Important: in case of date/time related data types (ie DateTime, TimeSpan, Week and Month) all delays added/subtracted to dynamic minima/maxima are TimeSpan.

It has the following properties to be configured:

SMinimum
An invariant-culture string representation of the fixed minimum, if any. Also possible an expression based on Now or Today in case DateTime (explained later on in this page).
SMaximum
An invariant-culture string representation of the fixed maximum, if any. Also possible an expression based on Now or Today in case of DateTime (explained later on in this page).
DynamicMinimum
A string containing a representation of all dynamic minima together with their added/subtracted tolerances, if any. Format is explained later on in this page.
DynamicMaximum
A string containing a representation of all dynamic maxima together with their added/subtracted tolerances, if any. Format is explained later on in this page.
Propagate
A boolean that affects behavior just during client side validation. When it is set, instead of signalling a validation error, the value is automatically changed to satisfy all constraints. If there is no value satisfying all constraints a validation error is signalled anyway. The constraint satisfaction algorithm is run each time the user changes a dynamic limit.

String format of DynamicMinimum and DynamicMaximum

Minimums/Maximum definitions in the list are separated by one or more spaces. Each definition consists of a property name, with an optional tolerance. When provided the tolerance is preceeded by either + or -, then an ! marks the start of an invariant culture string representation of the tolerance. Tolerances preceeded by + are added while tolerances preceeded by - are subtracted. Below a few examples:

"ADateMax+!1.00:00:00 AnotherDateMax", "ADoubleMax+!1.5+AnotherDoubleMax-!1.0". Where "1.00:00:00" is the TimeSpan string representation of one day.

For property nested in sub-objects the nested notation is used: "FatherProperty.ADateMax".

Now/Today format of fixed Minimum and MAximum

Now/Today relative minima/masxima have the format : SMinimum="Now-1h+3d..." or SMinimum="Today+1h-3d..." where: s: seconds; m: minutes; h: hours; d:days; M: months;y: years. This format may be used only with DateTime.

Error message

Error message coming from any source is split on the '|' char to get four messages:

  1. first message used when both fixed Minimum and Maximum are not null
  2. second message used when just fixed Minimum is not null
  3. third message used when just fixed Maximum is not null
  4. fourth message used when both fixed Maximum and fixed Mainimum are null
If messages are less than 4, all situations that are not covered by a specific messages use the last available message.


Fork me on GitHub