Created
April 15, 2012 11:38
-
-
Save AlexBar/2392169 to your computer and use it in GitHub Desktop.
Custom model validator for MvcExtensios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BoolRequired : CustomModelValidator | |
| { | |
| private readonly Func<string> _getErrorMsg; | |
| public BoolRequired(ModelMetadata metadata, ControllerContext controllerContext, Func<string> getErrorMsg) | |
| : base(metadata, controllerContext) | |
| { | |
| _getErrorMsg = getErrorMsg; | |
| } | |
| public override IEnumerable<ModelValidationResult> Validate(object value, object model) | |
| { | |
| if (!IsValid(value)) | |
| { | |
| yield return new ModelValidationResult | |
| { | |
| Message = _getErrorMsg() | |
| }; | |
| } | |
| } | |
| private bool IsValid(object value) | |
| { | |
| return value != null && (bool)value; | |
| } | |
| public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() | |
| { | |
| yield return new ModelClientValidationRule { ValidationType = "brequired", ErrorMessage = _getErrorMsg() }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class BoolRequiredModelMetadataItemBuilderExtensions | |
| { | |
| public static ModelMetadataItemBuilder<bool> BoolRequired(this ModelMetadataItemBuilder<bool> self, string errorMessage) | |
| { | |
| return BoolRequired(self, () => errorMessage); | |
| } | |
| public static ModelMetadataItemBuilder<bool> BoolRequired(this ModelMetadataItemBuilder<bool> self, Func<string> errorMessage) | |
| { | |
| self.ValidateBy((m, c) => new BoolRequired(m, c, errorMessage)); | |
| return self; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function ($) { | |
| $.validator.unobtrusive.adapters.addBool("brequired", "required"); | |
| })(jQuery) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class CustomModelValidator : ModelValidator | |
| { | |
| protected CustomModelValidator(ModelMetadata metadata, ControllerContext controllerContext) : base(metadata, controllerContext) | |
| { | |
| } | |
| public sealed override IEnumerable<ModelValidationResult> Validate(object container) | |
| { | |
| return Validate(Metadata.Model, container); | |
| } | |
| public abstract IEnumerable<ModelValidationResult> Validate(object value, object model); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment