Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Enable unobtrusive validation for specific submit button

I have two submit buttons Back, Continue. What should I to do to disable client validation when I click on Back. I was trying to add cancel class to button attribute but It seams does not help.

UPD. Actually this is working cancel class. But It seams not working if you add it dynamically(by javascript).

like image 566
Sergii Avatar asked Nov 21 '11 11:11

Sergii


People also ask

How to disable button in form with validation?

Use the disabled=”@(! context. Validate()) attribute for the submit button component to validate the form to display and enable or disable the button. If an Error message occurs in form validation, the button is disabled.

How do you disable client-side validation?

To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.

How do you remove data annotation validation on client-side?

To disable client side validation, we need to disable it by force. Notice the @data_val= “false”. It will disable the validation on this field.

What is jQuery unobtrusive validation?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.


2 Answers

I attached an event handler to certain buttons, that altered the settings of the validator object on that particular form.

$(".jsCancel").click(function (e) {
    $(e.currentTarget).closest("form").validate().settings.ignore = "*"
});

This has worked like a charm for me in MVC3.

I don't know if this helps you in particular, but since I use ajax form, I had to attach the event to these buttons each time the contents of the ajax form was replaced, by using the event ajax success. The full code that reparses the form and attaches the event to the cancel buttons is:

$(document).ajaxSuccess(function (event, xhr, settings) {
    var $jQval = $.validator, adapters, data_validation = "unobtrusiveValidation";
    $jQval.unobtrusive.parse(document);
    $(".jsCancel").click(function (e) {
        $(e.currentTarget).closest("form").validate().settings.ignore = "*"
    });
});
like image 179
Skymt Avatar answered Sep 19 '22 01:09

Skymt


Hijack the button click for form submission using JavaScript.

Here is good example with jQuery:

$("#MyButton").click(function(e) { 

    //submit your form manually here
    e.preventDefault();
});
like image 44
tugberk Avatar answered Sep 21 '22 01:09

tugberk