Replace default client side validation with custom validation in ASP.NET MVC

I have a View Model that contains a property DateTime

for which I want to provide a textbox using a custom format (month and year only, "MM .YYYY"):

public class MyModel {
  public DateTime? DateField {get; set;}
}

      

Formatting the value for the TextBox is easy (using a format string). I have also implemented a custom converting binder and it works great.

I still have a client side validation problem: I can implement a custom validator derived from ValidationAttribute

that implements IClientValidatable

and configures the appropriate jquery.validate adapters, etc.

But MVC still adds a default validation attribute data-val-date

(in addition to my custom validation attribute data-val-monthyeardate

) in the input field, so the default validation is still applied and the "MM.YYYY" input is rejected.

Is there a way to suppress the default client side validation for the datatype and replace it with a custom one (instead of "adding" a custom one)?

+3


source to share


3 answers


Since the struct does not allow you to override the real type with any custom attribute, or even override ModelMetadataProvider

for certain types, you will have to register your own global ModelMetadataProvider

one that tricks the validator into thinking it is actually a string

.

Something like:

public class MyModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        if (attributes.OfType<ExcludeCharAttribute>().Any())
            modelType = typeof (String);

        return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); ;
    }
}

      

Register it using:



ModelMetadataProviders.Current = new MyModelMetadataProvider();

      

However, since the main problem is a client-side problem, I would consider it exclusively with client-side code. In your adapter, monthyeardate

you can forcibly remove the validation date

(I can give an example if you share your code monthyeardate

).

See MSDN

+2


source


These are hacks, but one simple thing you could do is add:

$(function () {
    $.validator.methods.date = function () { return true; }; 
});

      



Thus, the default data-val-date always returns true along with invoking your custom date validation.

+1


source


I know this is a bit older, and just in case someone is not thinking about it (and since I cannot leave comments) to strengthen @ AlexC's answer, you can add confirmation to this statement.

For example, I am using moment.js for date validation ( moment.js ) and this allows you to add your own validation rules.

if (moment(a, "M/YYYY").isValid() || moment(a).isValid())
{
    return true;
}

      

This will check if it is a regular date and also in this case if the date is in the "M / YYYY" format. If one of them is true, it receives confirmation.

0


source







All Articles