Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

FormValidation.js with Repeater Form


Hi all, for the form validation is bind with attribute "name" only. If i have repeater form how do i validation with it ?

For example:

username: {
validators: {
notEmpty: {
message: "Username not empty"
}
}
}

if i put username[] will get error


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi,

At the moment we do not have this example so we will check this further and implement this and provide you a working example the soonest.

Regards.



Hi,

Please follow the below code to implement this:

JS:

// Define form element
const form = document.getElementById("kt_docs_repeater_form");

// Init form validation rules. For more info check the FormValidation plugin"s official documentation:https://formvalidation.io/
var validator = FormValidation.formValidation(
form,
{
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
rowSelector: ".fv-row",
eleInvalidClass: "",
eleValidClass: ""
}),
excluded: new FormValidation.plugins.Excluded({
excluded: function (field, ele, eles) {
if (form.querySelector("[name="" + field + ""]") === null) {
return true;
}
},
}),
}
}
);

const addFields = function(index) {
const namePrefix = "data[" + index + "]";

// Add validators
validator.addField(namePrefix + "[name]", {
validators: {
notEmpty: {
message: "Text input is required"
}
}
});

validator.addField(namePrefix + "[email]", {
validators: {
emailAddress: {
message: "The value is not a valid email address"
},
notEmpty: {
message: "Email address is required"
}
}
});

validator.addField(namePrefix + "[primary][]", {
validators: {
notEmpty: {
message: "Required"
}
}
});
};

const removeFields = function(index) {
const namePrefix = "data[" + index + "]";

validator.addField(namePrefix + "[name]");
validator.addField(namePrefix + "[email]");
validator.addField(namePrefix + "[primary][]");
}

$(form).repeater({
initEmpty: false,

show: function () {
$(this).slideDown();

const index = $(this).closest("[data-repeater-item]").index();

addFields(index);
},

hide: function (deleteElement) {
$(this).slideUp(deleteElement);
}
});

// Initial fields
addFields(0);

// Submit button handler
const submitButton = document.getElementById("kt_docs_repeater_button");
submitButton.addEventListener("click", function (e) {
// Prevent default button action
e.preventDefault();

// Validate form before submit
if (validator) {
validator.validate().then(function (status) {
if (status == "Valid") {
// Show loading indication
submitButton.setAttribute("data-kt-indicator", "on");

// Disable button to avoid multiple click
submitButton.disabled = true;

// Simulate form submission. For more info check the plugin"s official documentation: https://sweetalert2.github.io/
setTimeout(function () {
// Remove loading indication
submitButton.removeAttribute("data-kt-indicator");

// Enable button
submitButton.disabled = false;

// Show popup confirmation
Swal.fire({
text: "Form has been successfully submitted!",
icon: "success",
buttonsStyling: false,
confirmButtonText: "Ok, got it!",
customClass: {
confirmButton: "btn btn-primary"
}
});

//form.submit(); // Submit form
}, 2000);
}
});
}
});


HTML:
https://gist.github.com/KeenthemesHub/a5ac1af938ce6c6ddbbf072afb62a7ce

We will add this example to the docs in the next v8.1.9 update that we are planning to release in a few days.

Regards.



When dealing with a repeater form or an array of form fields, you can modify the validation configuration to accommodate the specific geometry dash subzero structure.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(