Need to write a dialog validation for number of multifield entires | Community
Skip to main content
Level 3
May 14, 2024
Solved

Need to write a dialog validation for number of multifield entires

  • May 14, 2024
  • 2 replies
  • 1082 views

I have a component wherein, I have a container that has multifield which is representing rows in a table. We should be able to add 10 sets of rows, after which if we attempt to add another row, a custom error message must appear. Are there any OOO validations for this scenario? If not, are there any references to custom validations?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by avesh_narang

#path-to-your-multifield  means html element having multifield .

something like this : $(element).is("coral-select")

where coral-select is the element how multifield renders .

2 replies

PGURUKRISHNA
Level 4
May 14, 2024

Hi @pixislinger 

In Adobe Experience Manager (AEM), you can create a custom dialog validation for the number of multifield entries using JavaScript. Here's a step-by-step guide to implementing this validation:

  1. Create a custom dialog for your component.
  2. Add a multifield to the dialog.
  3. Add a clientlib to your project (if you don't have one already) and include the path to the clientlib in your component's dialog.

Now, let's add the validation logic:

  1. In your clientlib, create a JavaScript file (e.g., multifield-validation.js).
  2. Add the following code to the JavaScript file:

(function ($, $document) {
'use strict';

var maxRows = 10;

function validateMultifield() {
var multifieldPath = '#path-to-your-multifield';
var multifieldRows = $(multifieldPath + ' > .coral-Form-fieldset');

if (multifieldRows.length > maxRows) {
alert('Error: Maximum rows (' + maxRows + ') reached.');
return false;
}

return true;
}

$document.on('coral-dialog-open', function () {
$('.cq-dialog-submit').on('click', function (e) {
if (!validateMultifield()) {
e.preventDefault();
}
});
});

})($, $(document));

 

Replace '#path-to-your-multifield' with the actual path to your multifield.

  1. Include the clientlib in your component's dialog.

Now, when you open the dialog and attempt to add more than 10 rows, an alert will appear displaying the custom error message, and the form submission will be prevented.

For more information on AEM dialogs and clientlibs, you can refer to the following resources:

These resources will help you understand how to work with AEM dialogs and clientlibs, and how to implement custom validations tailored to your specific use case.

 

Thank you 🙂

Level 3
May 15, 2024

What does #path-to-your-multifield mean here? Could you give an example? Is it the one under cq:dialog?

avesh_narang
avesh_narangAccepted solution
Level 3
May 19, 2024

#path-to-your-multifield  means html element having multifield .

something like this : $(element).is("coral-select")

where coral-select is the element how multifield renders .