Tag: mvc

  • What is Data Annotations Extensions?

    What is Data Annotations Extensions?

    Data Annotations Extensions MVC C# Visual Studio

    Data Annotations extensions are for model validation, both on client and server-side.

    Well, these are great if you’re looking for something simple and fast. Don’t use them if you’re working on a project with many fields and dependencies. If you’re doing complex validation, I would highly recommend writing custom validation. If you’re familiar with NuGet, please search for it using Visual Studio Nuget package manager. Then install it inside your solution using Visual Studio. I wasn’t going to show any complex examples because the author has the examples on the website.

     

    Data Annotations Extensions

    • CreditCardAttribute
    • CuitAttribute
    • DateAttribute
    • DigitsAttribute
    • EmailAttribute
    • EqualToAttribute
    • FileExtensionsAttribute
    • IntegerAttribute
    • MaxAttribute
    • MinAttribute
    • NumericAttribute
    • UrlAttribute
    • YearAttribute

    Here is how you would use a data annotation extension attribute on your model.

    [Email]
    public string UserEmail {get; set;}

    After your model is hooked up to an attribute, you can then use your controller and views as normal. Just make sure you include your jQuery Validate library on your views. Then enable unobtrusive validation on your web config inside your solution project folder. It might be already enabled if you’re using a built-in template to start your project. Once you fire up your website and browse to the page that has the
    “UserEmail” field, try typing in some sort of email. The new data annotation extensions on the field will try to validate the input. It works exactly the way it would with the default attributes. If the value passes validation then no error will be shown. Otherwise, if the value is wrong, then an error will be display and user will not be able to submit the form.

    That’s pretty much it, good luck on using data annotations extensions within your MVC projects using Visual Studio and C#.

    You might also want to read

  • How to Reset Form Unobtrusive jQuery?

    How to Reset Form Unobtrusive jQuery?

    I was looking around for a way to reset a form that was already validated by Unobtrusive jQuery Client Validation. JQuery validate doesn’t have any way to reset the UI after errors occur. However, you can safely use:

    var validator = $( "#myform" ).validate();
    validator.resetForm();

    The best example I found online was on John Culviner blog. It wasn’t exactly what I was looking for, but it was a good start. He resets the form by using a button and attributes, I need a function that does that, so I create a plugin that clears the form on demand.

    Clear Reset Form Unobtrusive jQuery validation part 2

    usage: $(“#signupform”).clearForm()

    (function ($) {
    
        $.fn.clearForm = function (options) {
    
            // This is the easiest way to have default options.
            var settings = $.extend({
                // These are the defaults.
    
                formId: this.closest('form')
    
            }, options);
    
            var $form = $(settings.formId);
    
            //reset jQuery Validate's internals
            $form.validate().resetForm();
    
            //reset unobtrusive validation summary, if it exists
            $form.find("[data-valmsg-summary=true]")
                .removeClass("validation-summary-errors")
                .addClass("validation-summary-valid")
                .find("ul").empty();
    
            //reset unobtrusive field level, if it exists
            $form.find("[data-valmsg-replace]")
                .removeClass("field-validation-error")
                .addClass("field-validation-valid")
                .empty();
    
            return $form;
        };
    
    }(jQuery));

    Good luck on your adventure! This should help a few people using unobtrusive Validation with MVC3.