Expand my Community achievements bar.

SOLVED

Need to write a dialog validation for number of multifield entires

Avatar

Level 4

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?

1 Accepted Solution

Avatar

Correct answer by
Level 3

#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 .

View solution in original post

4 Replies

Avatar

Level 4

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

Avatar

Level 4

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

Avatar

Correct answer by
Level 3

#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 .