I am trying to dynamically disable the parsley.js max length on several of my input fields when the user makes a selection on a select list on the form.
I have read this thread, but when I put the code into my field, the parsley is not triggered, instead the form submits, and I do not understand why.
I have read the parsley.js docs, but I am unable to see why the parsley.js validation is ignored when i add the following line of code:
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '0');
or
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '150');
This is my code to dynamically turm the parsley validation on and off when the user changes the select list on the form:
function toggleFormDetails() {
if ( $('#id_employment_record_display_type').val() == '8888' || $('#id_employment_record_display_type').val() == '9999' ) {
//disable the input field.
$('#id_employment_record_position_title').prop('disabled', true);
....
//destroy parsley on the form.
//$('#employment_history_details_form').parsley().destroy();
//disable the parsley maxlength, when the input field is disabled.
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '0');
//reinitialise parsley on the form.
//$('#employment_history_details_form').parsley();
} else {
//enable the input field.
$('#id_employment_record_position_title').prop('disabled', false);
....
//destroy parsley on the form.
//$('#employment_history_details_form').parsley().destroy();
//change the parsley cs error values for all the required form inputs.
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '150');
//reinitialise parsley on the form.
//$('#employment_history_details_form').parsley();
}
}
Why do I have have to add the destroy & create parsley code on the form (I have commented them out above)?
Would it be better to write a custom validation for this? If so, how would I do that, b/c my js code skills are not yet good enough?