[英]Parsley js form validation with dropkick - Cannot add error class on custom select dropdown
I am using dropkick custom dropdown plugin to customize my select
elements and Parsley js
for form validation. 我正在使用dropkick自定义下拉插件来自定义我的
select
元素和Parsley js
以进行表单验证。 On form validation, the error message does get displayed under the dropdown but error
class is not being added. 在表单验证中,错误消息的确显示在下拉菜单下,但未添加
error
类。
I know that dropkick
hides the <select>
and generates its own custom <div>
and <ul>
for displaying the custom dropdown. 我知道
dropkick
隐藏<select>
并生成自己的自定义<div>
和<ul>
来显示自定义下拉列表。 So is there a way to add error message class to these dynamically generated <div>
by dropkick? 那么有没有办法通过dropkick向这些动态生成的
<div>
添加错误消息类?
Here is my code: 这是我的代码:
HTML: HTML:
<form class="form-inline row" id="quote_form_header">
<select class="dropkick_select"
data-parsley-required="true"
data-parsley-required-message="State is required.">
<option selected disabled>State</option>
<option>State1</option>
<option>State2</option>
</select>
<button type="submit">GET A QUOTE</button>
</form>
JS: JS:
<script>
$(function(){
//Initialize dropkick on the form select elements
$(".dropkick_select").dropkick();
//Parsley js validation code
var parsley_valiation_options = {
errorTemplate: '<span class="error-msg"></span>',
errorClass: 'error',
}
if ($('#quote_form_header').length > 0)
$('#quote_form_header').parsley(parsley_valiation_options);
})
</script>
Finally, found the solution: DEMO 最后找到解决方案: DEMO
Here is the code which I made and worked: 这是我制作和工作的代码:
var parsley_valiation_options = {
errorTemplate: '<span class="error-msg"></span>',
errorClass: 'error'
}
$('form').parsley(parsley_valiation_options)
window.Parsley.on('field:error', function() {
var element = $(this.$element);
// This global callback will be called for any field that fails validation.
//console.log('Validation failed for: ', this.$element);
//If the select element is a dropkick select, then add error class to dropkick generated custom <div> element
if ($(this.$element).hasClass('dropkick_select')) {
//var el = $(element).siblings('div.dk-select').first();
$('.dk-selected').addClass('error');
}
});
//Initialize dropkick on the form select elements
$(".dropkick_select").dropkick({
change: function() {
var select_elem = this.data.elem;
var selected_value = this.value;
//console.log("selected_value = "+selected_value);
//if selected value is not blank and if validation error message is currently being displayed under the select element, then remove the error message
if (selected_value != "" && $(select_elem).siblings('ul.parsley-errors-list').length > 0) {
$(select_elem).siblings('ul.parsley-errors-list').children('span').remove();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.