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.
Comments: Hi The compiler is getting confused because you're mixing types in the rule definition. The rule is defined on an Int16, but the comparison values (1950 and 2030) have been defined as ints. If you cast them to int16's it should work fine. RuleFor(x => x.ModelYear).InclusiveBetween((Int16)1950, (Int16)2030) Jeremy
[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.
Comments: Hi The compiler is getting confused because you're mixing types in the rule definition. The rule is defined on an Int16, but the comparison values (1950 and 2030) have been defined as ints. If you cast them to int16's it should work fine. RuleFor(x => x.ModelYear).InclusiveBetween((Int16)1950, (Int16)2030) Jeremy