Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show error details when throwing Validator Exception

I have code in a managed bean:

public void setTestProp(String newProp) {
   FacesMessage yourFailure = new FacesMessage();
   yourFailure.setDetail("Really you need to promise to never do that again!");
   yourFailure.setSummary("Stop here, now!");
   yourFailure.setSeverity(FacesMessage.SEVERITY_FATAL);
   throw new ValidatorException(yourFailure);
}

and in the XPage:

<xp:messages id="messages1" layout="table" showSummary="false"
    showDetail="true" globalOnly="false">
</xp:messages>

but I get as result message (nicely in the yellow box as expected, not in an error page):

Error setting property 'testProp' in bean of type com.ibm.sg.demo.Test: javax.faces.validator.ValidatorException: Stop here, now!

I would like to:

  • not have the technical part
  • see the summary

What do I miss?

like image 840
stwissel Avatar asked May 28 '26 17:05

stwissel


1 Answers

The problem is that the property resolver catches all java.lang.Throwable from the get/set method of the managed beans. The "original" facesMessage is replaced with a new one (the previous message is appended).

You have three possibilities:

  1. Create your own property resolver
  2. Create your own validator and attach it to the field binded to the managed bean
  3. Add a validation method to your bean

Hope this helps

Sven

EDIT:

How to add a validation method to your bean

a) Add a validation method to your bean

public void validate(FacesContext context, UIComponent toValidate,  Object value){

    // Do your validation with value
    // if everything is ok, exit method

    // if not, flag component invalid...
    ((UIInput)toValidate).setValid(false);

    // ... create your message ...
    FacesMessage yourFailure = new FacesMessage();
    yourFailure.setDetail("Really you need to promise to never do that again!");
    yourFailure.setSummary("Stop here, now!");
    yourFailure.setSeverity(FacesMessage.SEVERITY_FATAL);

    context.addMessage(toValidate.getClientId(context),  yourFailure);
}

b) Add your validator to the field

<xp:inputText id="inputText1"
value="#{TBean.test}"
validator="#{TBean.validate}">

(You can name the method whatever you want.)

This validator has not to be added to the faces-config.xml.

like image 167
Sven Hasselbach Avatar answered May 30 '26 06:05

Sven Hasselbach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!