1
I Use This!
Activity Not Available

News

Posted about 11 years ago by kf78
I see where I got confused. Apparently there's another product out there that uses and extends FluentValidation. This is the document I was looking at: https://github.com/ServiceStack/ServiceStack/wiki/Validation Thanks for the speedy reply! I'll try to read online examples more carefully in the future. :)
Posted about 11 years ago by JeremyS
Hi This isn't actually a feature that's ever existed. I'm curious where you saw it, because I don't remember ever having heard of this idea before. Anyway, you could probably implement it by using a custom IValidatorInterceptor implementation. ... [More] From within an interceptor you can get access to both the controller context and the validator context, which would allow you to specify a ruleset based on the http method. Jeremy [Less]
Posted about 11 years ago by kf78
I see references to an ApplyTo flags to define different RuleSets for different HTTP methods back in version 3, but I don't seem to see any corresponding functionality in the current version. Did this go away? Is there a better/easier way to do this? Thanks!
Posted about 11 years ago by MotoWilliams
Happy to help if/once I understand how to do it. Being able to use FV with MVC & Web Api with a container seems like it would a common use-case for a lot of folks.
Posted about 11 years ago by JeremyS
So at the moment it isn't really possible in this way. RuleSets cascade down to child validators, but you can't explicitly choose a ruleset to execute. I really like this idea though, so if you could create an issue for this on the issue tracker I'll try and add it for a future release. Jeremy
Posted about 11 years ago by JeremyS
Hi No. Validators are completely separate from mvc, and have no knowledge of any mvc-specific items such as controllers or tempdata. The only thing that the validator sees is the object being passed to it for validation, so you could copy the items ... [More] you need from tempdata into properties on the model. These could then be accessed from within the validator. Jeremy [Less]
Posted about 11 years ago by gadd68
Thanks Jeremy, think I finally figured it out, making sure it only runs if fields are not empty/null RuleFor(x => x.ExpireYear).Must(x => Convert.ToInt32(x) >= DateTime.Now.Year).When(f => ... [More] !String.IsNullOrEmpty(f.ExpireYear)).WithMessage(expMsg); RuleFor(x => x.ExpireMonth).Must(x => Convert.ToInt32(x) >= DateTime.Now.Month).When(f => !String.IsNullOrEmpty(f.ExpireMonth) && !String.IsNullOrEmpty(f.ExpireYear) && Convert.ToInt32(f.ExpireYear) == DateTime.Now.Year).WithMessage(expMsg); Hopefully it might help someone else. [Less]
Posted about 11 years ago by pauljame
Hello Is it possible to access controller TempData in a Validator ? Thanks
Posted about 11 years ago by JeremyS
Hi A call to RuleFor should reference a property directly. You can't call Convert.ToInt32 from within a rule definition. You'll either need to 1) change the data type of your ExpireMonth and ExpireYear properties to be ints. or 2) Use a custom ... [More] Must rule that performs the conversion, rather than relying on GreaterThanOrEqualTo. Example: // Assuming ExpireYear is still a string RuleFor(x => x.ExpireYear).Must(x => Convert.ToInt32(x) >= DateTime.Now.Year) Basically, you can't mix numeric validators with string properties, so you need to make sure you do the conversion somewhere (either before FV is invoked, or within the validator, but not within the RuleFor) [Less]
Posted about 11 years ago by gadd68
Hi, I'm new to Fluent Validation. I know I should be able to do this, and have looked up lots of other posts but just can't figure it out. ExpireMonth and ExpireYear are strings This bit works fine RuleFor(x => ... [More] x.ExpireMonth).NotEmpty().WithMessage("Blah Blah")); RuleFor(x => x.ExpireYear).NotEmpty().WithMessage("Blahh")); Now if the Expiry Month & Year are not empty I want to make sure that the Expiry date is still current. So the logic is Make sure the year is at least this year If the year is this year, make sure the month is not less than the current month I know with the method I first came up with below I get the "Property name could not be automatically determined for expression x => Convert.ToInt32" but I can't figure how to fix the rule below and also make sure it only runs if the values are not Empty RuleFor(x => Convert.ToInt32(x.ExpireYear)) .GreaterThanOrEqualTo(DateTime.Now.Year) .WithMessage("The credit card expiry date is invalid. Please check the values you have entered"); RuleFor(x => onvert.ToInt32(x.ExpireMonth)) .GreaterThanOrEqualTo(DateTime.Now.Month) .When(x => Convert.ToInt32(x.ExpireYear) == DateTime.Now.Year) .WithMessage("The credit card expiry date is invalid. Please check the values you have entered"); Any help appreciated [Less]