Do the attributes RequiredIfRegExMatch and RequiredIfNotEmpty work with Decimals?

Jul 28, 2011 at 2:32 PM

I've got a controller with properties using Foolproof validation but neither of them work. I'm not sure if it's because they're nullable decimals or not, but neither validation attribute seems to work.

 

        [Display(Name = "Nominal Capacity")]
        [RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
        [Range(0, 9999.99, ErrorMessage = "Value must be between 0 - 9,999.99")]
        public decimal? NominalCapacity { getset; }

               [Display(Name = "unit of measure")]
               [RequiredIfRegExMatch("NominalCapacity"@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Unit of Measure Required when Nominal Capacity has a value")]
               public int NominalCapacityUnitOfMeasureID { getset; }

 

        [Display(Name = "Overflow Capacity")]
        [RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
        [Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
        public decimal? OverFlowCapacity { getset; }
 
        [Display(Name = "unit of measure")]
        [RequiredIfNotEmpty("OverFlowCapacity", ErrorMessage="Unit of Measure Required when Overflow Capacity has a value.")]
        public int OverflowCapacityUnitOfMeasureID { getset; }
Coordinator
Aug 9, 2011 at 3:28 PM

Is this a clientside issue? If so I found a bug in the unobtrusive js files. It might be your problem. 

See: 77989 

Aug 9, 2011 at 4:16 PM

I ended up getting RequiredIfNotEmpty to work, but I found an issue with the RegEx matching. It seems that RegularExpression,  ("[0-9]*\.?[0-9]+") will work 'completely' on my values if I typed them as strings, but not as decimal or decimal?.   When I say completely, I mean it will validate the value 0.5 but not .5 unless the underlying type of my property is a string.  If I make it a decimal, I have to have a leading 0.  This could have been the case with RequiredIfRegExMatch, but I found the check to be redundant as I was already validating the value on the property, so RequiredIfNotEmpty worked.