Show #NotNull message in p:tooltip - jsf

I was wondering if one could access the validation error message assigned via Bean validation through the UIInput's binding.
For example, i have the following property:
#NotNull(message = "{username_required}")
private String username;
When I print the error using h:message, i get the appropriate message printed out, so I know that Bean Validation works correctly. I would like to print out the message without h:message tag, supplying the value directly to a tooltip, like this:
<p:tooltip for="usernameErrorImage" rendered="#{!usernameInputBinding.valid}"
value="#{!usernameInputBinding.requiredMessage}"/>
The 'rendered' attribute works as expected, however the value of the tooltip is 'true', which is not my message. I have also tried 'convertedMessage' and 'requiredMessage', but both of them also return the string 'true'. Putting the h:message inside of the tooltip tag prints out the message, however I would like to know if I can access the validation message directly through the binding.

Provided that you've a ValidationMessages.properties with among others the username_required entry, just use <f:loadBundle> the usual way to load it as a resource bundle in JSF view.
<f:loadBundle basename="ValidationMessages" var="validationMessages" />
...
<p:tooltip ... value="#{validationMessages['username_required']}"/>
Grabbing the component's requiredMessage attribute as in your initial attempt will definitely not work as it only returns the manually definied requiredMessage attribute.

You can nest the message inside the tooltip.
This worked for me in Primefaces 4.
bean:
#NotNull(message = "{username_required}")
private String username;
xhtml:
<p:tooltip for="usernameErrorImage">
<p:message for="usernameErrorImage" />
</p:tooltip>

Related

jsf inputtext doesn't show value from bean

I have the follow situation:
I have a bean that send to form some data, but only in outputlabel the data from the bean is displayed.
I tried to use primefaces, but the same problems persist.
my code:
<h:outputLabel value="#{Bean.name}" id="name2" />
<h:inputText value="#{Bean.name}" id="name" />
<p:inputText value="#{Bean.name}" id="name3" />
Any idea why?
You should have given the bean's code also, to help us better analyze the problem.
Normally you should check for the following:
Check whether you are specifying a correct bean name. Normally
bean's name is same as that of class, except that first letter
should be lowercase. In your case it should be #{bean.name} or else,
specify your custom name with #Named("Bean").
Check whether the getters and setters such as getName() are properly
provided. It may happen that you might have reset the name property in
your bean in the get method itself. Because of which first time it
shows you properly in outputLabel and then in next call to getName it may give you null or empty String. To check this, try put your inputText tag first, and check.
I solve my problem.
When I tried show the values, I was trying recover data from database by pass an ajax action. So, When I clicked at button to retrieve the datas, some of my inputText were set as a required. And because this the data is just displaying into label and not inside of inputtext with required. But because ajax, the request were not called correctly.
When I remove the required from inputtext, it works fine.

Primefaces and tag value from bean method

I want to set the value of a primefaces tag from a bean method using arguments but this doesn't work.
On the Facelets page:
<p:outputLabel id="userLabel" value="#{languageBean.retrieveLanguage(1)}" />
<p:commandButton value="#{languageBean.retrieveLanguage(2)}"
action="#{loginBean.logIn()}"
update="loginForm"/>
On the bean:
public String retrieveLanguage(int key) {
return (String) getPageMap(pagePath, pageName).get(key);
}
I get the following exception:
javax.faces.view.facelets.FaceletException: Error Parsing /components/login.xhtml: Error Traced[line: 24] Element type "p:outputLabel" must be followed by either attribute specifications, ">" or "/>".
Could you give me any idea, please?
Regards,
Roberto
You shouldn't be passing in the language value from the front page - this is moving actual login from the app into the graphical frontend. Rather, store the language value in the bean and simply call retrieveLanguage.
If you are trying to handle translations, have a look at resource bundles instead.

Data in <h:inputText readonly="true"> disappears when command button is clicked

I am using JSF 1.1. I have a JSF page with a request scoped bean and a readonly input field.
<h:inputText id="dt" value="#{bean.sdate}" readonly="#{bean.disable}" />
<a onclick="cal('dt');"><img src="fr.gif" border="0"></a>
When I set the input value using JavaScript and click on command button, then the data in input field disappears.
How is this caused and how can I solve it.
That's because the property is set to readonly. If this evaluates true, then JSF won't process the submitted value and hence the model won't be updated. If you want to set it to readonly on rendering the view and have JSF to process the submitted value, then you'd need to make it to evaluate true on render response phase only. You can use FacesContext#getRenderResponse() for this. You'd need to do this in your isDisable() method.
public boolean isDisable() { // TODO: rename to isReadonly().
return FacesContext.getCurrentInstance().getRenderResponse();
}
Note: in JSF2 you could access FacesContext#getCurrentInstance() by #{facesContext} in the view as well, this saves some boilerplate in the model:
<h:inputText ... readonly="#{facesContext.renderResponse}" />
Also note that when you're using JSF2 <f:viewParam>, then this approach won't work on GET requests anymore. See also Make a p:calendar readonly for the explanation and workaround.

UIComponent#getValue() obtained via binding is not available in validator of another component

I'm trying to figure out why an f:attribute tag's value isn't passed when attached to h:inputSecret tag. I'm quite new to jsf, but as far as I know attributes can be attached to any kind of component. Here is the code:
<h:inputSecret id="passw" value="#{advertAdder.userPass}"
required="true" validator="#{advertAdder.validatePasswords}">
<f:attribute name="confirmedPass" value="#{advertAdder.passConfirmator.value}"/>
</h:inputSecret>
<h:inputSecret id="passwConfirm" required="true"
binding="#{advertAdder.passConfirmator}"/>
and the method that wants to acces this attribute:
public void validatePasswords(FacesContext context, UIComponent component, Object value)
{
if (!value.equals(component.getAttributes().get("confirmedPass")))
{
FacesMessage mess = new FacesMessage("Password and it's confirmation are not the same!");
context.addMessage(component.getClientId(context), mess);
((UIInput) component).setValid(false);
}
}
In above code component.getAttributes() always returns map with only two attributes:
javax.faces.component.VIEW_LOCATION_KEY and com.sun.faces.facelets.MARK_ID.
I've added attribute tag to a h:commandButton to check it, and then everything was fine. Am I missing something or it's not possible to add an attribute to non-action tag?
I'm using Mojarra 2.0.2 and Glassfish 3.0.1.
Thanks in advance.
Input components are processed in the order as they appear in the component tree. The UIInput#getValue() is only available when the component is already been processed. Otherwise you need to use UIInput#getSubmittedValue() instead.
<f:attribute name="confirmedPass" value="#{advertAdder.passConfirmator.submittedValue}"/>
Note that this gives you the unconverted and unvalidated value back. It would make somewhat more sense to put the validator on the confirm password field instead and pass the value of the first password field along. See also JSF Validator compare to Strings for Equality and JSF doesn't support cross-field validation, is there a workaround?
Alternatively, you can also try out the OmniFaces <o:validateEqual> component. You can find a concrete example in this article.
Unrelated to the concrete problem, it's unnecessary to bind the component to the bean this way. Replace all occurrences of #{advertAdder.passConfirmator} by #{passConfirmator}. Keep the controller free of properties which are never internally used.

Why is a dynamic created JSF EL value expression not resolved?

I got a simple setup (and a big issue): a JSP page with en empty panel grid item container and a binding to a bean.
<h:panelGrid binding="#{ bean.container }" id="container" />
When the getter of the bean will be called, the container is filled with a random number of columns with a command link inside. So far so good. The container is filled up with the right number of elements, and with the use of an ActionListener on the links, I get all click events.
Here comes the tricky part: I want to mark the 'selected' or 'pressed' column via a different style class. With a static setup, I would do this with an expression like:
<h:column styleClass="#{ bean.selectedColumn eq 'id' ? 'btnSelected' : 'btn' }">
<!-- command link and some blahblah -->
</h:column>
The bean contains a simple getter getSelectedColumn() , that returns an id. Straight forward, so this works perfect!
But when I try to do the same inside the bean,
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
String expression = "#{ bean.selectedColumn eq 'id' ? 'btnSelected' : 'btn' }";
new ExpressionFactoryImpl().createValueExpression(elContext, expression, String.class);
column.setValueExpression("styleClass", valueExpression);
the expression won't ever be resolved. To make myself clear: both the command links, the columns and the value expressions are generated inside the bean. Is that the cause?
Can anyone tell me why? Thanks in advance!
When the JSP is compiled the bean wont be called! This is done at runtime cause you want to see live data in the bean. Therefore the (later) generated EL is not visible at compilation. The EL would not be resolved at runtime.

Resources