1

I'm attempting to use the DecimalMin validator from NHibernates Validator library. Unfortunately, due to compiler restrictions I'm getting errors when attempting to utilize it. I'm sure its something simple, but I'm at a point where I need to move onto another task and not burn anymore time on it so any help would be appreciated.

    [DecimalMin(10.01)]
    public decimal Amount { get; set; }

The code above uses the DecimalMin attribute with the constructor value of 10.01. The constructor of the Attribute accepts decimal values. However, the compiler will not allow 10.01 or 10.01m.

With 10.01 as the value I receive: "Cannot convert source type 'double' to target type 'decimal'

With 10.01m as the value I receive: "An attribute argument must be a constant expression, type of expression or array creation expression of an attribute parameter type."

I've googled around to try and find an answer and my google-fu is failing me today.

Does anyone have an example of how to use this validator?

Donn Felker
  • 9,553
  • 7
  • 48
  • 66

1 Answers1

2

This is a bug in NHibernate.Validator. Decimals are not representable in attribute parameters.

use decimal values as attribute params in c#?

The solution is to patch NHibernate.Validator so that the constructor takes a float or double and then perform the conversion to decimal in the ctor. Wish I had a better answer for you.

Community
  • 1
  • 1
James Kovacs
  • 11,549
  • 40
  • 44
  • James - Thanks! Much appreciated. I thought I was going to have to write my own, doing exactly what you recommended. I just wanted to double check that I wasn't losing my mind. Thanks again! – Donn Felker Nov 17 '10 at 22:08
  • Filed a bug and patch for the issue with the NHibernate team: http://216.121.112.228/browse/NHV-107 – James Kovacs Nov 19 '10 at 02:36