Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't actually recommend using the FluentValidationModelMetadataProvider - this was only really ever an experimental addition (which very well may be removed from the next release), and it doesn't support any of the class-level DataAnnotations (such as DisplayColumn). I would suggest that you use FluentValidation only for validation, but stick with attributes for metadata.</p> <p>That being said, if you really want to get this working then you could implement by using a custom no-op validator that's used only for metadata:</p> <pre><code>public static class MetadataExt { public static IRuleBuilderOptions&lt;T, TProperty&gt; DisplayColumn&lt;T, TProperty&gt;(this IRuleBuilder&lt;T, TProperty&gt; rule) { var ruleBuilder = (FluentValidation.Internal.RuleBuilder&lt;T, TProperty&gt;)rule; ruleBuilder.Rule.AddValidator(new DisplayColumnWrapper(ruleBuilder.Rule.PropertyName)); return ruleBuilder; } public class DisplayColumnWrapper : NoopPropertyValidator, IAttributeMetadataValidator { private string name; public DisplayColumnWrapper(string name) { this.name = name; } public override IEnumerable&lt;ValidationFailure&gt; Validate(PropertyValidatorContext context) { return Enumerable.Empty&lt;ValidationFailure&gt;(); } public Attribute ToAttribute() { return new DisplayColumnAttribute(name); } } } </code></pre> <p>... Which you could then use like this:</p> <pre><code>public class Validator : AbstractValidator&lt;SomeModel&gt; { public Validator() { RuleFor(x =&gt; x.DisplayColumnProperty) .DisplayColumn(); } } </code></pre> <p>You'd then need to create a custom ModelMetadataProvider that knows how to process this:</p> <pre><code>public class ExtendedFVModelMetadataProvider : FluentValidationModelMetadataProvider { IValidatorFactory _validatorFactory; public ExtendedFVModelMetadataProvider(IValidatorFactory validatorFactory) : base(validatorFactory) { this._validatorFactory = validatorFactory; } public override ModelMetadata GetMetadataForType(Func&lt;object&gt; modelAccessor, Type modelType) { var validator = _validatorFactory.GetValidator(modelType); if (validator == null) { return base.GetMetadataForType(modelAccessor, modelType); } // Only look for the DisplayColumnWrapper // There is a mismatch as MVC expects this to be defined at class-level, but FV defines everything at the property level. var displayColumns = from memberWithValidator in validator.CreateDescriptor().GetMembersWithValidators() from propertyValidator in memberWithValidator let wrapper = propertyValidator as MetadataExt.DisplayColumnWrapper where wrapper != null select wrapper.ToAttribute(); var displayColumn = displayColumns.FirstOrDefault(); // we found a displaycolumn, so pass it over to MVC to build the metadata. if (displayColumn != null) { return CreateMetadata(new[] { displayColumn }, null /* containerType */, modelAccessor, modelType, null /* propertyName */); } return base.GetMetadataForType(modelAccessor, modelType); } } </code></pre> <p>The provider overrides the GetMetadataForModel method and looks for any properties that use DisplayColumn. This could probably be extended if you want to support any other custom metadata extensions too. You could then use this provider in place of the metadata provider that comes with FluentValidation. </p> <p>However, I still wouldn't recommend this approach...the library is designed for performing validation, not for generating UI metadata. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload