1
I Use This!
Activity Not Available

News

Posted about 11 years ago by nkapur
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 ... [More] 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.MvcIf 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() +293what am I doing wrong here? Microsoft's data annotations handles this right out of the box. [Less]
Posted about 11 years ago by JeremyS
So I spent several hours stepping through the WebApi code to try and figure out what was going on, and unfortunately I can't find a way around it. Looking at the code for JQueryMvcFormUrlEncodedFormatter, it seems that when creating the model ... [More] binder it completely disregards all configured values and creates a new HttpConfiguration (in FormDataCollectionExtensions.ReadAs in the WebApi source). Because of this, it seems impossible to customize the validation behaviour, because even though I can swap out components in the Services collection, these are completely ignored by the time that model binding occurs. I can't begin to imagine why this design decision was taken. So basically I'm stuck - I can't find another way around this without changing the mvc source code. [Less]
Posted about 11 years ago by JeremyS
Some more detail on your initial problem: the unit tests weren't picking this up as they were using json for all their input. It looks like the different formatters behave differently - so if you're just using a standard form post rather than sending json, then the "a value is required" message is generated, which is why I missed originally.
Posted about 11 years ago by JeremyS
OK, so this seems to come from WebApi in the situation when no value was provided for a non-nullable value type (in this case, Age as it's a integer), which is similar to how MVC works in this situation. You can work around this by making the Age ... [More] property nullable (thereby allowing empty values). The other approach that's supposed to work, is by explicitly defining a NotNull rule for Age, then you can override the message with one of your own. However, this doesn't seem to be working with WebApi which looks like a bug. It might take me a while to get this sorted because of my unfamiliarity with WebApi, but I'll try and dedicate some time to it next week. To be honest, the integration for both MVC and WebApi are in a mess because the extensibility points that Microsoft provide for 3rd party validation libraries are very limited (it also doesn't help that there are subtle differences between MVC4 and MVC5, and not-so-subtle differences between MVC and WebApi when it comes to validation). It's been my plan for a while to rewrite the validation provider for both of these, and get some integration tests written around them. I've created a separate issue for this: [https://fluentvalidation.codeplex.com/workitem/7179] I'll try and posts updates on that issue rather than here. [Less]
Posted about 11 years ago by JeremyS
The MVC/WebApi integration sucks and needs re-doing:1) FV takes the approach of "set all the properties, then validate the whole object in one go", whilst MVC and WebApi take the approach of "validate each value, and only set the property if it's ... [More] valid". I've tried to have the MVC/WebApi integration do the former for consistency with FV, but after many years of painfully trying to support this, I think it's time to bow to the MVC/WebApi approach :(2) The codebases are too separate. Try and share as much code as possible for the two, keeping in mind that MVC and WebApi are being merged together in asp.net vnext (ditching the MVC pipeline and using a pipeline much more similar to WebApi)3) The tests suck. Ditch the unit tests in favour of integration tests. Trying to simulate the model binder/validation infrastructure is a pain and doesn't work well. It would be much better if they actually ran through the entire MVC/WebApi pipeline. Definitely do this for WebApi, although the MVC tests might need to wait for asp.net vnext. [Less]
Posted about 11 years ago by rocco337
Jeremy, I created test project, you can download it from http://www.sendspace.com/file/zhhb99. This is test request $.ajax({ url: 'http://localhost:60233/api/Values/Post', type: "POST", data: 'Name=&Age=' }); Response that I got from this request is "A value is required.,Required field" Roko
Posted about 11 years ago by JeremyS
I'm still having trouble reproducing this - in my unit tests if I leave the response empty then no additional validation errors are generated. Any chance you can put together a small sample that reproduces this? I can then investigate further. ... [More] There isn't a actually a signed package for FluentValidation.WebApi at present, you'll need to compile your own if you want one. I'm actually strongly considering stopping producing the signed packages altogether, so it'll probably go away for all the packages for the next major release of FluentValidation. Jeremy [Less]
Posted about 11 years ago by rocco337
Jeremy thank you for fast responses and for you cooperation. One more thing, I needed FluentValidation.WebApi-Signed nuget. Nuget search can't find it. I found it by googling it. http://www.nuget.org/packages?q=FluentValidation.WebApi-Signed https://www.google.hr/search?q=FluentValidation.WebApi-Signed
Posted about 11 years ago by rocco337
[DataContract] public class ConfirmPaymentRequest { [DataMember] public string TransactionId { get; set; } [DataMember] public string CustomerAuthenticationToken { get; set; } } Class looks something ... [More] like this and request is empty, and rule is also simple: this.RuleFor(m => m.CustomerAuthenticationToken).NotEmpty(); [Less]
Posted about 11 years ago by JeremyS
Could you provide some sample data (as json) and a class definition that reproduces the issue? Thanks