I have a plugin which generically handle the creation of a jQuery dialog used for CRUD operations. The markup of the form that is added to the dialog is available externally of the plugin code and the plugin just ask to an http service to provide the markup and when received simply add it to the dialog itself.
I have then created a callback in the plugin (onSetupValidation
function) which is meant to be the handle for the plugin user to customize the validation for each form. This is an example of code I am using
var element = $('#mydiv');
element.crudplugin({
[...]
onSetupValidation: function(markup) {
var form = $('#myForm', markup);
var container = $('<div class="error"><p>Errors were found validating your form. Please correct them and retry.</p><ol></ol></div>')
.appendTo(form).hide();
var validator = form.validate({
errorContainer: container,
errorLabelContainer: $('ol', container),
errorElement: 'em',
wrapper: 'li',
highlight: function (element) {
$(element).addClass("ui-state-error");
},
unhighlight: function (element) {
$(element).removeClass("ui-state-error");
},
submitHandler: function (form) {
[...]
}
});
}
[...]
});
Well. Let's go to the problems
- If I add the validation rules inside the markup (as class attributes) validation does not work at all. Same behaviour if I add the rules in the
validate()
method Googling around I have found many samples, expecially here on SO, that uses the
.rules('add', rule)
method of the validation plugin like in the following sample and all these links recommends to call thevalidate
method before adding any rule like in the following sample$("#myField", markup).rules("add", { required: true });
But if I use this method when the istruction get executed I get the following error:
SCRIPT5007: Unable to get value of the property 'settings': object is null or undefined jquery.validate.min.js, line 15 character 257
Any advice?