1

I tried to set a calendar as invalid from the Java bean. The code is:

HtmlInputText htmlInput = (HtmlInputText) elem;
String classes = htmlInput.getStyleClass();
String newClasses = classes + " ui-state-error";
htmlInput.setStyleClass(newClasses);

UIInput input = (UIInput) elem;
input.setValid(false);

elem is the calendar. The class is NOT displayed in the browser inspector.

In theory, input.setValid(false); should be sufficient... Maybe the JS of p:calendar is doing something wrong?

The element is:

<p:calendar 
    value="#{bean.val}"
    id="x"
    locale="it"
    showOtherMonths="true"
    pattern="dd/MM/yyyy"
    navigator="true" 
/>

I use Primefaces 3.4.1 with Mojarra 2.1.7

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
  • Did you update the component or a parent? – Jasper de Vries Jul 14 '21 at 12:49
  • @JasperdeVries When the validation is finished, the whole form is updated. So, a parent. – Marco Sulla Jul 14 '21 at 13:22
  • 1
    Why do it the hard way by the way? Can't you just create a validator? – Jasper de Vries Jul 14 '21 at 13:35
  • A Validator seemed to me more complicated :D but now that you rememberd me it, I'll try and I'll return with feedback – Marco Sulla Jul 14 '21 at 13:38
  • @JasperdeVries: well, it works, but no error message is displayed, even if I specified it in the `ValidatorException` constructor. Usually I have to problem to show messages with `addMessage`. – Marco Sulla Jul 14 '21 at 15:08
  • @JasperdeVries: mmmmmhhh.... clicking more times, I discovered that the message is created, but hidden immediately! How is it possible? With "addMessage", messages are shown as usual for some seconds.... But even if I pu an addMessage inside the `validate`, it's behaviour is the same! – Marco Sulla Jul 14 '21 at 15:30
  • That's a different question, which is hard to answer without much context. – Jasper de Vries Jul 14 '21 at 15:32
  • Ok, I'll open another question. Can you answer to this question? I'll accept it. – Marco Sulla Jul 14 '21 at 15:33

1 Answers1

1

Don't try to reinvent the wheel. Instead, create a validator. It will take care of setting the valid state of the component, the error message and it is reusable.

See:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102