setting f:validatelength dynamically does not work - jsf

When I use a bean to set the min and max values on f:validatelength it simply does not work. It breaks the functionality in the form so it can't be submitted...
When I outputtext the values they do come out as legit numbers and hardcoding those numbers work fine.
My guess it that it somehow lacks the required connection to beans at that time in the lifecycle or something? (I'm pretty much a beginner). Example below is example of what I want to do
<h:inputSecret id="password" value="#{userBean.user.password}">
<f:validateLength minimum="#{myBean.minSize}" maximum="#{myBean.maxSize}" />
</h:inputSecret>
<h:message for="password" />
I tried using < a4j:keepAlive > on the bean but I couldn't get that to work.
I am using JSF 1.2 with Richfaces 3.3.3 and Seam 2.
Hopefully someone has a clue here. And a good way to achieve this using another tag then that's fine as well! Thanks for reading.

Is the el expression evaluated to integer type?

I have no idea, but you can try a couple of these variations
<h:inputSecret id="password" value="#{userBean.user.password}" maxlength="#{myBean.maxSize}">
<f:validateLength minimum="#{myBean.minSize}"/>
</h:inputSecret>
<h:message for="password" />
<h:inputSecret id="password" value="#{userBean.user.password}" maxlength="#{myBean.maxSize}">
<f:validator validatorId="javax.faces.validator.LengthValidator">
<f:attribute name="minimum" value="#{myBean.minSize}"/>
</f:validator>
</h:inputSecret>
<h:message for="password" />

Related

synchronize E mail automatically with database

I wanted to know from you whether it is possible to check an email entry, if it exists already in the database and to display a message .
This validation should work dynamically without a button.
I work with jsf 2.1 and prime faces 3.5
I hope you understand what I mean and thank you for your help.
You need to implement a custom validator. Full explanation in this guide
Snippet code:
<h:inputText id="email" value="#{bean.email}">
<f:validator validatorId="myEmailValidator" />
<f:ajax event="keyup" render="emailError" />
</h:inputText>
<h:message id="emailError" for="email" />

How to disable autocomplete in <h:inputText>?

I'm making my term paper using JSF 2.0, Java 6 and Tomcat 7, but I'm having some doubts in the development.
In my project I have a <h:inputText> tag
<h:inputText value="#{turmaMBean.nome}">
<f:ajax event="keyup" render=":meuForm:minhaTabela" />
</h:inputText>
I want to disable autocomplete. I already tried:
<h:inputText value="#{turmaMBean.nome}" autocomplete="false">
<f:ajax event="keyup" render=":meuForm:minhaTabela" />
</h:inputText>
However, it didn't work. What can I do to solve this problem?
I found the answer. I searched a lot about this issue and I discover this isn't an issue. When I try to do the following:
<h:inputText value="#{turmaMBean.nome}" autocomplete="off">
<f:ajax event="keyup" render=":meuForm:minhaTabela" />
</h:inputText>
I didn't restart my Tomcat Server, and by some strange way Tomcat doesn't updated my view properly. When I tried to restart the server, this attribute start work.
So, if you need to turn the autocomplete off, use:
autocomplete="off"
This probably will work - don't forget to restart the server, sometimes Tomcat like play with us :p.

Check for validation errors in part of a form in JSF

I'm building a Seam application, which is basically a huge form divided into different parts, or modules. I need a way to figure out when a module is "complete", meaning all validation for the fields in that module passes. I then need to do something in the view, setting a css-class or whatever.
Something like:
<a:region id="region1">
<s:div styleClass="#{invalid ? 'errors' : ''}">
<h:inputText required="true" id="input1" />
<h:inputText required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>
</a:region>
I figured I had two options:
Using some sort of view-logic (like #{invalid} for a single field)
Using a method in the bean, where I get all components for the module programmatically, and check them for validation errors.
However, I can't find any way to do any of them. Any ideas if this is even possible?
We're using JSF 1.2 with Seam.
Thanks.
You can use UIInput#isValid() to check if a validation error has occurred on the particular input component.
<s:div styleClass="#{!input1.valid or !input2.valid ? 'errors' : ''}">
<h:inputText binding="#{input1}" required="true" id="input1" />
<h:inputText binding="#{input2}" required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>

how to Validate double values with regular expression in jsf?

it should allow upto 30 digits before decimal and atleast 1 , atmost 2 digits after decimal
If you use JSF 2.0 you can use the regex-validator tag:
<h:inputSecret id="password" value="#{user.password}">
<f:validateRegex pattern="add your pattern here" />
</h:inputSecret>
If you are still on JSF 1.x I think you have to write your own custom validator by creating a class that implements javax.faces.validator.Validator interface. Here is a good tutorial how to achieve this.
Here in this example is the solution i suggest for validating double:
<h:inputText id="someField" value="#{yourBean.doubleValue}">
<f:convertNumber minFractionDigits="2" />
<f:validateDoubleRange minimum="1" maximum="30"/>
</h:inputText>
Use the attributes of the convertNumber tag to set the limits you need.

How to access html components in JSF EL?

I want some code in facelet (jsf 2.0) to work:
<h:inputText id="q" />
<h:button outcome="/search.xhtml?q=#{q.value}" />
but when I press the button, search page opens without any parameters.
I think, my EL expression is wrong. Is it possible to access inputText value from EL expression? If not, how can I achieve the same functionality?
I've finished with using plain html form:
<form action="faces/search.xhtml" method="get">
<input type="text" name="query" />
<input type="submit" value="Find" />
</form>
In search.xhtml I have view param to get a value of query string:
<f:metadata>
<f:viewParam name="query" />
</f:metadata>
This solution has the problem - "faces/search.xhtml" is hardcoded. Also, when I place this form in search.xhtml and perform several searches I have something like this in browser url:
"http://localhost:8080/Application/faces/faces/faces/search.xhtml"
I think this problem can be solved with PrettyFaces (TODO :)
this is a late answer but I guess many people finding this post will have the same doubt whether EL can access html component and how it can:
Yes, EL can access JSF component which are implicit JSF object.
However, up to now I have only seen and used the example of component.valid:
<h:inputText id="streetNumber" value="#{property.streetNumber}" title="streetNumber" required="true" requiredMessage="Please enter the street number!" validatorMessage="Please enter a street number between 1 and 1000" converterMessage="Please enter a number" pt:placeholder="900">
<f:convertNumber integerOnly="true"/>
<f:validateLongRange minimum="1" maximum="1000" />
</h:inputText>
<h:message for="streetNumber" class="#{!streetNumber.valid ? 'label label-warning' : 'none'}" />
So, I suggest you(Dmitry) change the code to
<h:button outcome="#{"/search.xhtml?q=" + q.value}" />
Please let me know whether it works because I am also curious and anyone who faces the similar problem can follow my example and please let me know the result.

Resources