Elfayer
Elfayer

Reputation: 4561

How to handle h:inputText validation?

I'd like to display an error next to the fields that are wrong. I have that code for my page :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/templates/basic.xhtml">
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="3">
                <h:outputText value="Firstname"/>
                <h:inputText id="firstName" value="#{account.firstName}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="firstName" errorStyle="color:red; display:block"/>
                <h:outputText value="Lastname"/>
                <h:inputText id="lastName" value="#{account.lastName}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="lastName" errorStyle="color:red; display:block"/>
                <h:outputText value="Login"/>
                <h:inputText id="login" value="#{account.login}" required="true">
                    <f:validator validatorId="stringAlphaValidator"/>
                </h:inputText>
                <h:message for="login" errorStyle="color:red; display:block"/>
                <h:outputText value="Password"/>
                <h:inputText id="password" value="#{account.password}" required="true">
                </h:inputText>
                <h:message for="password" errorStyle="color:red; display:block"/>
                <h:outputText value="Address"/>
                <h:inputText id="address" value="#{account.address}" required="true">
                    <f:validator validatorId="stringAlphaNumericValidator"/>
                </h:inputText>
                <h:message for="address" errorStyle="color:red; display:block"/>
                <h:outputText value="Email"/>
                <h:inputText id="email" value="#{account.email}" required="true">
                    <f:validator validatorId="emailAddressValidator"/>
                </h:inputText>
                <h:message for="email" errorStyle="color:red; display:block"/>
            </h:panelGrid>
            <h:commandButton value="Register"/>
            <h:messages globalOnly="true"/>
        </h:form>
    </ui:define>
</ui:composition>
</html>

If a field is empty and I press the Register button, I get that (here all fields are empty):

Error while empty fields

(sorry the error is in french. I don't know if it comes from my eclipse language or my tomcat server)

I didn't write this ! This is a text that my program writes on it's own somehow... How do I remove this ?

Also, this is a register page, I'd like to add to my DB the user (if the fields are right) and then change of page with the action login. How can I call a method to do that before it changes of page ?

If you need to see more code, like the validator classes, I can add it.

Upvotes: 1

Views: 32822

Answers (2)

Elfayer
Elfayer

Reputation: 4561

I managed it this way (personnaly) :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/templates/basic.xhtml">
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="3">
                <h:outputText value="Firstname"/>
                <h:inputText id="firstName" value="#{account.firstName}" required="true" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z)">
                    <f:validateRegex pattern="[a-zA-Z]+"/>
                </h:inputText>
                <h:message for="firstName" errorStyle="color:red; display:block"/>
                <h:outputText value="Lastname"/>
                <h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z)">
                    <f:validateRegex pattern="[a-zA-Z]+"/>
                </h:inputText>
                <h:message for="lastName" errorStyle="color:red; display:block"/>
                <h:outputText value="Login"/>
                <h:inputText id="login" value="#{account.login}" required="true" requiredMessage="The field is empty" validatorMessage="String is not valid (only characters a-z A-Z 0-9)">
                    <f:validateRegex pattern="[a-zA-Z0-9]+"/>
                </h:inputText>
                <h:message for="login" errorStyle="color:red; display:block"/>
                <h:outputText value="Password"/>
                <h:inputSecret id="password1" value="#{account.password1}" required="true" requiredMessage="Enter a password">
                </h:inputSecret>
                <h:message for="password1" errorStyle="color:red; display:block"/>
                <h:outputText value=""/>
                <h:inputSecret id="password2" value="#{account.password2}"/>
                <h:outputText value=""/>
                <h:outputText value="Address"/>
                <h:inputText id="address" value="#{account.address}" required="true" requiredMessage="The field is empty" validatorMessage="Wrong address (eg.: 42bis My address)">
                    <f:validateRegex pattern="[a-zA-Z0-9]+ [a-zA-Z ]+"/>
                </h:inputText>
                <h:message for="address" errorStyle="color:red; display:block"/>
                <h:outputText value="Email"/>
                <h:inputText id="email" value="#{account.email}" required="true" requiredMessage="The field is empty" validatorMessage="The email is not valid">
                    <f:validateRegex pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"/>
                </h:inputText>
                <h:message for="email" errorStyle="color:red; display:block"/>
            </h:panelGrid>
            <h:commandButton value="Register" action="#{account.action}"/>
            <h:messages globalOnly="true"/>
        </h:form>
    </ui:define>
</ui:composition>
</html>

Upvotes: 1

Patrick
Patrick

Reputation: 4572

This should work.

<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">

And the fields you validate, you can add a validatorMessage="<Message>"

** UPDATE * If you want to use Bean Validation, like described in my comment,go this way:

// for example your NamesBean
@Named(value = "namesBean")
@SessionScoped
public class NamesBean {

// attributes

public void setFirstName(String firstName) {
 this.firstName = firstName;
}

@Size(min=3, max=10, message="Min 3 and max 10 characters")
public String getFirstname() {
 return firstName;
}

}

and the xhtml

<h:message for="lastName"/>
<h:inputText id="lastName" value="#{account.lastName}" required="true" requiredMessage="Please enter your Lastname">

Thats it.

Upvotes: 7

Related Questions