f:validateRegex pattern for > 0.0 - validation

Before we start I want to inform you that I have done a lot of searching and have looked at all of the "Questions that may already have your answer", all over Google, and the <f:validateRegex pattern="" /> forms and the can not come across; how, by using <f:validateRegex pattern="" /> to make sure the entered value is greater than 0.0.
I am making a BMI calculator using PrimeFaces and I need this validation.
Here is the code where I am asking for the input:
<p:inputText id="height"
value="#{peopleData.height}"
size="6"
required="true">
<f:validateRegex pattern="[0-9+]+.+[0-9]+"/>
</p:inputText>
I was going to set pattern="[0-9+]+.+[1-9]+" but then the user would always need to have could not have a height of anything like *.0
I am absolutely lost with this and any suggestions would be awesome! Even if the solution would involve using something other than <f:validateRegex/>

Regex'es are mostly usefull for String validtion. As you request a Float/Double as input, try:
<f:validateDoubleRange minimum="0.1" maximum="..." />
This tag only allows digits and numeric values within the min-max-range.
See also: http://www.tutorialspoint.com/jsf/jsf_validatedoublerange_tag.htm

Related

Displaying the same image several times with multiple p:graphicImage without multiple HTTP requests

I'm using p:graphicImage tag in my XHTML page to displaying the same image in small an large :
<f:facet name="header">#{msgs.ATTR_PICTURE}</f:facet>
<h:panelGroup>
<p:graphicImage id="product_thumbnail" styleClass="thumbnail"
cache="false" value="#{imageBean.streamedImageById}">
<f:param name="productId" value="#{_product.id}" />
</p:graphicImage>
<p:tooltip id="imagebigger" for="product_thumbnail" position="right" showDelay="0" showEffect="blind" styleClass="tooltip_thumbnail">
<p:graphicImage value="#{imageBean.streamedImageById}" styleClass="thumbnail_large" cache="false">
<f:param name="productId" value="#{_product.id}" />
</p:graphicImage>
</p:tooltip>
</h:panelGroup>
I would like to avoid the 2 HTML requests systematically for each image to display. Is there a way to avoid that ?
You could use the stream attribute and set it to false.
This will render the image in base64 instead of rendering the URL to the image.
I don't suggest it as the markup will be very big and probably much worse performance.
I you remove the cache="false" from the first p:graphicImage (it defaults to true) the image is retrieved in the first <img ... /> (which the p:graphicImage renders to) on the client and just displayed from the client cache for the second one without retrieving it twice.
The cache="false" might have been there for a reason like you mentioned in the comments, but that is effectively a new question for which there are several Q/A in Stackoverflow but which one is the most appropriate depends on the details of your use case. Your new question might either be a duplicate again or a new valid question on its own... It might even be that returning the cache="true on the first p:graphicImage and replacing the second one with something else (even some pure client-side 'duplication' e.g.) might be a good approach.

How to add a converter to parameterized messages in JSF 1.2?

I'm looking for a way to convert my params in a parameterized text inside a JSF 1.2 xhtml page.
Let's say I have this parameterized message in my messages.properties (please ignore the stupidity of this example):
EXAMPLE_TEXT=Right now {0} Euro is {1} US-Dollar.
And I would like the output to look like this:
Right now 1 Euro is 1.3658 US-Dollar.
Then my xhtml code looks like this:
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" />
<f:param value="#{backingBean.dollarValue}" />
</h:outputFormat>
I already have implemented a converter (let's say its ID is bigDecimalConverter) that converts numeric values to the particular formats.
My question now is: (How) can a converter be registered to the output's param values?
I imagine something like this (which unfortunately seems to not be possible):
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" converter="bigDecimalConverter" />
<f:param value="#{backingBean.dollarValue}" converter="bigDecimalConverter" />
</h:outputFormat>
Asking google I found this thread, but it's no sufficient answer to my specific problem.
Thanks a lot in advance for any help!
You can have exact the same output with outputText:
1) Change output to <h:outputText value="#{messages.TEXT1} #{backingBean.euroValue} #{messages.TEXT2} #{backingBean.dollarValue} #{messages.TEXT3}" />
2) Create method in backing bean which return full String and call it in output text.
(This answer was transformed from previous comment)

Get the selected values from a rich:select which is inside o a4j:repeat

I am using a a4j:repeat control to iterate throughtout a Map<Object, List<MyType>> list. The XHTML page displays both h:outputText and rich:select controls.
I loop throughout a Map<String, List<Items>> instance, to show a master-detail tables. In the h:outputText I show the master description and the rich:select shows the detail.
The code is as follows:
customer.xhtml
<a4j:repeat value="#{masterManagedBean.listMasterItems.entrySet().toArray()}"
var="itemsVar">
<h:outputText value="#{itemsVar.key}" />
<rich:select enableManualInput="true">
<f:selectItems value="#{itemsVar.value}" id="itemsMenu"
var="itemsVarSelect"
itemLabel="#{itemsVarSelect.descriptionItems}"
itemValue="#{itemsVarSelect.idItems}" />
</rich:select>
</a4j:repeat>
This snippet works perfect. But, I don't have any idea how to gain the selected value from every rich:select generated by the repeater. Please guidance me to solve this issue.
Basically I would suggest not to use the datastructure Map<String,List> for the described case. Why not changing the Structure into a normal List of objects (List<SelectionObject>) with the SelectionObject-class holding:
name/label
possible values (as List or Array)
selected value
So the JSF code would look something like
<a4j:repeat value="#{masterManagedBean.listMasterItems}"
var="itemsVar">
<h:outputText value="#{itemsVar.label}" />
<rich:select enableManualInput="true" value="#{itemsVar.selectedValue}">
<f:selectItems value="#{itemsVar.possibleValues}" id="itemsMenu"
var="itemsVarSelect"
itemLabel="#{itemsVarSelect.descriptionItems}"
itemValue="#{itemsVarSelect.idItems}" />
</rich:select>
</a4j:repeat>
Any preparations/building for this object structure can be done e.g. in getListMasterItems (use some caching), the retrieval of the users data is done in the action of your submit-button, anyway. Just iterate through the list and read out the `selectedValue. If the answer is usable/helpful, please consider an upvote, confirm if it worked out for you.
Hope it helps...

Is it better to use JSF HTML or JSF Core namespaces with inputText when performing validation?

I need to perform simple form validation using JSF. Looking at different online examples I see two patterns:
<h:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</h:inputText>
...or...
<f:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</f:inputText>
The only difference is the namespace of the inputText tag. Both solutions seem to work, but which one is "more correct" and what makes it a better choice?
There is no such thing f:inputText, inputText is a part of the http://java.sun.com/jsf/html namespace and should use the h prefix,
like this: xmlns:h="http://java.sun.com/jsf/html"
Where have you seen the <f:inputText anyway ?
The only way that you can use <f:inputText is only if you assign the f prefix your self instead of the h , like this: xmlns:f="http://java.sun.com/jsf/html" (but its really not a good idea) better stick to the original h

Multiple h:inputTextarea to single back-end variable

I'm trying to create a comment page with a variable set of comments (layed out via o:tree, thanks BalusC) that anyone can reply to. I'm displaying the h:inputTextArea via p:inplace, so there are multiple textinputareas per page with multiple commandbutton replies. I'm trying to map the command button to a single back-end variable from a specific textinputarea so that every textinputarea isn't processed each time the command button is pressed.
Edit: Code example
<o:tree value="#{xPost.post.comments.treeModel}" var="comment" varNode="node">
<o:treeNode>
<o:treeNodeItem>
<p:panel>
<h:outputText value="#{comment.commentText}" />
<p:inplace label="Reply">
<br/>
<p:editor value="#{post.newComment}" />
<p:commandButton action="#{post.saveComment('#{comment.ID'})}" value="Comment" ajax="false" />
</p:inplace>
<o:treeInsertChildren />
</p:panel>
</o:treeNodeItem>
</o:treeNode>
</o:tree>
To add to this, I'm using hibernate validation, which to my knowledge can only validate via annotations:
#NotBlank(message="Please enter a comment.")
#Length(min=1, max=10000, message="A comment must be between 1 and 10,000 characters.")
#SafeHtml(message="Invalid Rich Text.")
private String newComment = "";
So from the code above, when I have 2+ p:editor, each editor is being processed and populating the same back-bean variable. How do I force a commentButton to only validate a specific inputBox/p:editor and set the backing-bean variable.
Thank you for your help.
Just give each editor its own form. In other words, move the <h:form> from outside the <o:tree> to inside the <o:treeNodeItem>. This will ultimately render multiple forms with each its own editor and button.
You need to provide your source details for people to give you more help
In your case, I'm guessing the following should do the trick:
<ui:repeat var="comment" ...> // or equivalent like o:tree
<h:inputText value="#{comment.message} .../>
<h:commandButton actionListener="#{bean.updateMessage}">
<f:setPropertyActionListener from="#{comment}" to="#{bean.commentBeingProcessed}"/>
<!-- Or alternatively, you can set the comment object on to the command button
and receive it on the server side as event.getComponent().getAttribute("comment")
<f:attribute name="comment" value="#{comment}"/>
-->
</h:commandButton>
</ui:repeat>
Hope that helps.

Resources