0

I have created a custom Validator, and it works with the usual p:message. But I would use this validator for more than one field of the same type (integer) and show the message in a growl or in a messages. But I noticed that this message is hided almost instantly in both growls and messages.

This is my growl (in the template):

<p:growl id="msgs" autoUpdate="true" showDetail="true" />

This is the messages (in the specific page):

<p:messages id="messages" autoUpdate="true" closable="true" showDetail="true" />

What's wrong? Normally, with addMessage, the template growl works. I tried also to do an addMessage in the validate() method of the Validator, but now two messages disappears!

I also tried to add sticky="true" to the growl, but nothing.

I'm tempted to add all p:message to the top of the page and remove the others with JS...

I'm using Primefaces 3.4.1 with Mojarra 2.1.7

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
  • Try with `keepAlive="true"` – Jasper de Vries Jul 15 '21 at 10:19
  • @JasperdeVries: where, in the growl or in the validator? – Marco Sulla Jul 15 '21 at 10:21
  • @JasperdeVries: if it was in the growl, `keepAlive="true"` does not work :( – Marco Sulla Jul 15 '21 at 10:26
  • That's why I don't use auto update in growls. Also I prefer to show a general message when validation failed in the growl and details with each failed field. See https://stackoverflow.com/questions/42247737/add-global-message-when-field-validation-fails You can also Ajax update your growl from that listener. – Jasper de Vries Jul 15 '21 at 10:51
  • @BalusC: removing the autoUpdate="true" from the "global" growl works, but if I put the same validator on more than one input, the error message is displayed more times, even if I put `redisplay="false"` – Marco Sulla Jul 15 '21 at 12:37

1 Answers1

0

With the help of BalusC, I solved the problem.

First, I removed the autoUpdate="true" from the "global" growl and the "local" messages, as suggested by BalusC. This will have repercussions on the rest of the application, but it's not a problem :D

The problem is that if I add the validator to two or more inputs, the same message is displayed multiple times. So, to the submit button, I added

oncomplete="Util.removeErrorDuplicates('#messages');"

where the code of Util.removeDuplicates() is:

Util.removeErrorDuplicates = function (selector) {
    setTimeout(function() { 
        Util.removeDuplicates(selector + " li"); 
    }, 10);
}

and the code of Util.removeDuplicates() is:

Util.removeDuplicates = function (selector) {
    "use strict";
    
    var $elements = $(selector);
    var $element;
    var element;
    var html;
    
    var htmlsDone = new Set();
    var elementsNum = $elements.length;
    
    for (var i=0; i<elementsNum; i++) {
        element = $elements[i];
        html = element.outerHTML;
    
        if (htmlsDone.has(html)) {
            $element = $elements.eq(i);
            $element.remove();
        }
        else {
            htmlsDone.add(html);
        }
    }
}

Feel free to change the milliseconds in the setTimeout of Util.removeErrorDuplicates() if it's too low and duplicates are not removed :)

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
  • Did you read https://stackoverflow.com/questions/42247737/add-global-message-when-field-validation-fails? Seems like an easier and global fix for this issue. Note that you can use `globalOnly` on the growl. – Jasper de Vries Jul 15 '21 at 15:28
  • @JasperdeVries: yes, I read https://stackoverflow.com/questions/42247737/add-global-message-when-field-validation-fails . The difference is that I do not want custom message for any input (an overkill to create a class for ANY input). Furthermore, I tried to put `globalOnly="true"` but it does not work. – Marco Sulla Jul 15 '21 at 15:35