0

I was hoping to be able to do something like this:

 public class MinimumSalaryAttribute : ValidationAttribute
    {
        private readonly decimal _minValue;

        public MinimumSalaryAttribute(decimal minValue)
        {
            _minValue = minValue;
        }

        public override bool IsValid(object value)
        {
            return (decimal)value > _minValue;
        }
    }

and then in my view:

[MinimumSalary(0M)]
public decimal Salary { get; set; }

This would prevent users entering negative decimals. However, I get a compiler error: "not a valid attribute type". I understand the reason for this as described here: Why "decimal" is not a valid attribute parameter type?

What is the workaround in my vase?

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • 4
    Do you *really* need to use decimal here? Would you ever restrict a salary to 12.56? Just use `int`. Which means you can just use the normal `Range` attribute. – DavidG Aug 08 '17 at 10:38
  • 3
    Are you sure the `IsValid` is valid? I'd say it should return true if `value >= _minValue`... – Peter B Aug 08 '17 at 10:40
  • @PeterB, I have corrected this. Thanks. – w0051977 Aug 08 '17 at 10:42
  • @DavidG, surely a monetary value should be a decimal - that it what it is for - isn't it? Is your suggestion just a hack? Thanks anyway. – w0051977 Aug 08 '17 at 10:42
  • 1
    Well yes, it's a kind of a hack, but it's the least hacky way to achieve this. Only a hack will get around the fact you can't use a decimal for an attribute parameter. – DavidG Aug 08 '17 at 10:44
  • Why not just `[Range(0, double.MaxValue)]` –  Aug 08 '17 at 10:44
  • @Stephen Muecke, I was thinking about that. Are there any pitfalls to this? I cannot think of any. Thanks. – w0051977 Aug 08 '17 at 10:46
  • 1
    Only if its for Mark Zuckerberg :) –  Aug 08 '17 at 10:47

1 Answers1

1

You can use "Range" annotation; 10.50D is min value,50.80D is max value...

[Range(10.50D,50.80D,ErrorMessage ="Error min")]
public decimal Salary { get; set; }

Range attribute specifies the numeric range constraints for the value of a data field. It comes with System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

pisi1001
  • 89
  • 1
  • 5
  • Thanks. This appears to work. Could the downvoter explain what is wrong? – w0051977 Aug 08 '17 at 10:53
  • @w0051977 I would guess that the vote is because of the quality of the answer as it's just a code dump with no explanation of why it would work. – DavidG Aug 08 '17 at 10:57
  • @DavidG, If I saw this answer, then I would upvote it and I will. I find it clear and concise. Maybe another paragraph would of helped. Is there anything wrong with it in principle? Thanks. – w0051977 Aug 08 '17 at 10:59
  • @w0051977 Any post which is effectively only a code dump is not useful. This one answers your question but without explaining why you can use `double` or even that the numbers *are* `double`. So personally I don't think it's worthy of an upvote in it's current form. – DavidG Aug 08 '17 at 11:01
  • @DavidG, fair enough. – w0051977 Aug 08 '17 at 11:05
  • @w0051977 Of course, there's nothing stopping pisi1001 [edit]ing this answer to make it much better, after all that's the entire point of this site! – DavidG Aug 08 '17 at 11:06