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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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!
Filed a bug and patch for the issue with the NHibernate team: 216.121.112.228/browse/NHV-107

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.