Reputation: 149
I am using Client Side Validations rails gem v3.1.0 in my application. I have a form displayed in a jquery dialog which needs to be validated. Client state validation is enabled on that form. Everything works well with the validation, but when user cancels the dialog (without submitting), I would like to clear any existing validation errors. This way when user fires the dialog again, the validations errors are not present already on the form.
I have tried to achieve this by triggering the client state validation pass event for each element on dialog close - it works by removing the wrapping error fields and labels. One issue is that subsequent validations don't work after this code. I feel that bound events are removed and not triggered anymore.
Is there a better way of achieving this?
My jquery dialog:
$('#create_dialog').dialog({
height: 260,
width: 420,
title: "title",
buttons: {"<%= I18n.t("create") %>": function(e) {
$('#new_segment').submit();
},
Cancel: function() {
var form = $('#new_segment');
form.find('[data-validate]:input').each(function() {
$(this).trigger('element:validate:pass');
});
$(this).dialog("close");
}
}
});
Upvotes: 2
Views: 1214
Reputation: 12524
not a clean way but this works
/**
* Remove Embedded validation perfectly that is compatible with validation gem
* @param field
* @returns {boolean}
*/
function removeValidationMessage(field) {
var $field = $(field) || field;
/**
* First wrap the previously occurring label in wrapper div with class 'field_with_errors'
* Then Wrap the field within a wrapper div of same class
* Then add validation message after the field with class name 'message'
*/
if ($field.siblings('.message').length > 0) {
$field.parent().prev('.field_with_errors').find('label').unwrap();
$field.unwrap();
$field.next('.message').remove();
return true;
}
return false;
}
Upvotes: 1
Reputation: 149
https://github.com/bcardarella/client_side_validations/issues/328
From Brian Cardarella, developer of Client Side Validations gem:
Unfortunately right now there is not a clean way to do this. This will be changing in 4.0
Upvotes: 0