Skip to content

Commit

Permalink
added require_from_group validation for inputs with array names
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddhant committed Sep 4, 2019
1 parent 409f631 commit 99d7645
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
21 changes: 11 additions & 10 deletions demo/demo-with-params.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!DOCTYPE html>
<html>

<head>
<title>JQuery Validate Wrapper</title>
<meta charset = "utf-8">
Expand Down Expand Up @@ -47,7 +46,7 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
<script src="../jquery.validateWrapper.min.js"> </script>
<script src="../jquery.validateWrapper.js"> </script>
<script>
$(function () {

Expand All @@ -68,7 +67,7 @@
g_name_group: "first_name last_name",
},
require_from_group: {
rfg_dob_group: 2,
rfg_dob_group: 2
},
highlight: function (element, errorClass, validClass) {
if(jQuery(element).next().hasClass('input-group-append'))
Expand Down Expand Up @@ -111,10 +110,15 @@
else if (element.is('input[type=checkbox]') || element.is('input[type=radio]'))
element.closest('.form-check').parent().append(error);
else if (element.hasClass('required-from-group')) {
let lastElement = $('.rfg_dob_group:last');
if ($(element).attr('name') == $(lastElement).attr('name'))
error.insertAfter(lastElement.parent().parent());
} else if (element.hasClass('group-in-one')) {
if(element.hasClass('rfg_dob_group')) {
let lastElement = $('.rfg_dob_group:last');
if ($(element).attr('name') == $(lastElement).attr('name'))
error.insertAfter(lastElement.parent().parent());
}else{
error.insertAfter(element);
}
}
else if (element.hasClass('group-in-one')) {
let lastEl = $('.g_name_group:last');
error.insertAfter(lastEl.parent().parent());
} else
Expand Down Expand Up @@ -151,12 +155,10 @@
});
}
}

//this is my callback function
const submitData = (form, data) => console.log('Submit Data function is called..');
</script>
</head>

<body>
<div class="container">
<div class="clearfix" style="height:30px;"></div>
Expand Down Expand Up @@ -273,5 +275,4 @@
</div>
</div>
</body>

</html>
18 changes: 13 additions & 5 deletions jquery.validateWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* JQuery validateWrapper Plugin v1.19.1
* Works well for jQuery validate Plugin v1.19.1
* JQuery validateWrapper Plugin v1.19.2
* Works well for jQuery validate Plugin v1.19.2
* Copyright (c) 2019 Siddhant Naik
*
* Plugin Name: validateWrapper
Expand Down Expand Up @@ -133,10 +133,18 @@
if (i == j) {
if (plugin._messages[j] != '') {
$('.' + j).each(function () {
if ($("[name=" + this.name + "]", plugin.$_element).length) {
$("[name=" + this.name + "]", plugin.$_element).rules("add", {
messages: { 'require_from_group': plugin._messages[j] }
if($('input[name^="'+this.name+'"]', plugin.$_element).length) {
$('input[name^="'+this.name+'"]', plugin.$_element).each(function() {
$("#"+this.id, plugin.$_element).rules("add", {
messages: { 'require_from_group': plugin._messages[j] }
});
});
}else {
if ($("[name=" + this.name + "]", plugin.$_element).length) {
$("[name=" + this.name + "]", plugin.$_element).rules("add", {
messages: { 'require_from_group': plugin._messages[j] }
});
}
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions jquery.validateWrapper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 99d7645

Please sign in to comment.