1
I Use This!
Activity Not Available

News

Posted over 10 years ago by JeremyS
Hi Yes, this is totally possible. I'll try and answer some of the points you raised in that post: The name should be available as meta-data. If you don't want to use reflection you can get it from the Rule: context.Rule.Member.DeclaringType.Name ... [More] I had to supply a value to the base constructor which is incorrect for my needs Yeah, just pass in an empty string or a placeholder. We'll ignore it going forward. If only the IRuleBuilder<T,TProperty> had getter-properties that provided the meta-data. Actually it does. The IRuleBuilder itself is just syntactic sugar on top of the PropertyRule object, so only the "friendly" methods are directly exposed. However, if you call the Configure method on IRuleBuilder, then this gives you access to the underlying PropertyRule. Example below. The other thing you'll need to use is IStringSource - the interface is used for retrieving strings. The default implementation is based around standard resource files, but we can make a custom implementation that uses your localization mechanism: public class MyStringSource : IStringSource { string _className; string _propertyName; public MyStringSource(string className, string propertyName) { _className = className; _propertyName = propertyName; } public string GetString() { var key = String.Format("{0}.{1}.Regex", _className, _propertyName); string result = ResourceManager.GetObject(key); return result; } //These are used for metadata and can be ignored. public string ResourceName { get { return null; } } public Type ResourceType { get { return null; } } } ...we can then make use of this within your custom extension method. So you could do something like this: public static IRuleBuilderOptions<T, TElement> Matches<T, TElement>(this IRuleBuilder<T, TElement> ruleBuilder) { return ruleBuilder .SetValidator(new RegexValidator()) .Configure(rule => { string className = typeof(T).Name; string propertyName = rule.PropertyName; // Now set the error message source to our custom IStringSource rule.CurrentValidator.ErrorMessageSource = new MyStringSource(className, propertyName); }); } This calls the Configure method, which bypasses the fluent interface and gives you access to the underlying object model, giving you access to everything on the PropertyRule. The GetString method in the custom IStringSource implementation is called every time that a message needs to be generated, so this should work in your multi-threaded & multi-tenant environment too. Hope that helps Jeremy [Less]
Posted over 10 years ago by Mystagogue
I made a Stackoverflow post asking about fluent-validation handling of convention-based localized validation. What kind of solution is there for that problem? Also, there is another dimension to the problem that the Stackoverflow post did not ... [More] mention. The application I'm building is also multi-tenant aware. Each and every request it processes has a brand ID stored in CallContext. The resource file used for localization depends on the brand. For example, both regex validation strings and error messages can be unique per brand. So it is not sufficient to specify statically or globally the resource file. I need fluent validation to have a hook point that is consulted for each validation request...to specify the resource file. It needs to be thread-safe, so that concurrent, multi-tenant requests can execute the same fluent validation simultaneously without a race-condition regarding which resource file is being accessed. Can fluent-validation handle this? [Less]
Posted over 10 years ago by Kurtaclan
HAHA, whoops jquery validate was causing my issue, remove it and suddenly the page listened to my commands.
Posted over 10 years ago by Kurtaclan
How do I prevent a model from being validated when the form has the NoValidate tag, or some similar work around?
Posted over 10 years ago by JeremyS
Ah, good catch - I didn't spot that. Glad you got it working.
Posted over 10 years ago by Marcel81
Hi Jeremy, Wow, I have been looking at this for a long time when I realized I made a mistake in the When test. Instead of: .When(list => list != null) It should be: .When(x => x.List != null) And indeed, it is not even correct as I now ... [More] know the When condition applies to the whole rule... I am using CascadeMode.StopOnFirstFailure instead now. Thanks! [Less]
Posted over 10 years ago by JeremyS
Hi I can't reproduce this - in my tests the When condition is executed successfully. Please could you put together a unit test that reproduces the problem. (as an aside, you'll probably want to split this into 2 rules - the When condition will also be applied to the NotNull, meaning it will never execute) Thanks Jeremy
Posted over 10 years ago by Marcel81
I have a ViewModel with a List property. When I declare this rule, the list.Any() is still evaluated and throws an exception because it's null: RuleFor(x => x.List) .NotNull() .Must(list => list.Any()).When(list => list != ... [More] null).WithMessage("{PropertyName} is empty."); Even though documentation says the rule should not be executed. [Less]
Posted over 10 years ago by samdevx1029
How about PCL targeting .NET 4.5, WP8, Xamarin.iOS and Xamarin.Android? Currently, the package fails with following error: Install-Package : Could not install package 'FluentValidation 5.5.0.0'. You are trying to install this package into a project ... [More] that targets 'portable-net45+win+wpa81+Xamarin.iOS10+MonoAndroid10+MonoTouch10', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. I need to use some validation logic in my PCL that works across all 3 mobile platforms using Xamarin and was hoping to find a nuget package for that. [Less]
Posted over 10 years ago by alexfont_rim
If we use the FromUri attribute in the action parameter the validations stop working.``` [HttpGet] [Route("")] public bool Get([FromUri] ProductTypeRequest request) { if (!this.ModelState.IsValid) { ... [More] return false; } return true; } ```Although, it works for FromBody attribute.Comments: Thanks for taking the time to check that, I appreciate. That actually explains why inside or before it didn't work. Yes, I guess the idea is to unify both versions exactly to avoid those differences. Really looking forward to check that. [Less]