Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

Closed Unassigned: Error while using InclusiveBetween with Int16? [7180]

$
0
0
I have a property of type Int16? since in database it is defined as tinyint.

[Display(Name = "Model Year")]
public Int16? ModelYear { get; set; }

Now only the model year between 1950 to 2030 is valid and the value can come null as well, when the user doesn't knows model year.

If I use :
RuleFor(m => m.ModelYear).InclusiveBetween(1950,2030);
I get this error the error below during compilation itself.

Error 39
'FluentValidation.IRuleBuilderInitial<FMB.Web.Mvc.Models.EquipmentModel,short?>' does not contain a definition for 'InclusiveBetween' and the best extension method overload 'FluentValidation.DefaultValidatorExtensions.InclusiveBetween<T,TProperty>(FluentValidation.IRuleBuilder<T,TProperty?>, TProperty, TProperty)' has some invalid arguments G:\user\G10NK\FMB_Team_Project\FMB_System\FMB.Web.Mvc\Models\EquipmentModelValidator.cs 26 13 FMB.Web.Mvc

If I use:
RuleFor(m => (Int32)m.ModelYear).InclusiveBetween(1950,2030);

that works and validates but when I try to save in cases when model year is null I get the error.

Nullable object must have a value.
[InvalidOperationException: Nullable object must have a value.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
lambda_method(Closure , EquipmentModel ) +63
FluentValidation.Internal.<>c__DisplayClassd`2.<CoerceToNonGeneric>b__c(Object x) in c:\Projects\FluentValidation\src\FluentValidation\Internal\Extensions.cs:138
FluentValidation.Validators.PropertyValidatorContext.get_PropertyValue() in c:\Projects\FluentValidation\src\FluentValidation\Validators\PropertyValidatorContext.cs:52
FluentValidation.Validators.PropertyValidator.Validate(PropertyValidatorContext context) in c:\Projects\FluentValidation\src\FluentValidation\Validators\PropertyValidator.cs:64
FluentValidation.Internal.PropertyRule.InvokePropertyValidator(ValidationContext context, IPropertyValidator validator, String propertyName) in c:\Projects\FluentValidation\src\FluentValidation\Internal\PropertyRule.cs:339
FluentValidation.Internal.<Validate>d__10.MoveNext() in c:\Projects\FluentValidation\src\FluentValidation\Internal\PropertyRule.cs:227
System.Linq.<SelectManyIterator>d__14`2.MoveNext() +293

what am I doing wrong here? Microsoft's data annotations handles this right out of the box.




Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>