Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User input validation in managed bean problem(JSF 2.0 )

I am having some problems when i try to validate the user input from my JSF, in a managed beans. I recive the validation message in the console, but i dont see it in the page. I dont understand where is the problem.

This is the console output:

INFO: Inside validation method!! INFO: NO MATCH!!! INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=bRegForm:name[severity=(INFO 0), summary=(Your name cannot contain special characters), detail=(Your name cannot contain special characters)]

This is how i made the JSF input component:

<h:inputText id="name" value="#{registrationController.name}" validator="#{registrationController.validateName}" required="true">
        <h:message for="name"/>
    </h:inputText>

And this is how how i created the validation method:

@ManagedBean
@RequestScoped
public class RegistrationController {
...
public void validateName(FacesContext context, UIComponent validate,
        Object value) {
    String inputFromField = (String) value;
    String simpleTextPatternText = "^[a-zA-Z0-9]+$";
    Pattern textPattern = null;
    Matcher nameMatcher = null;
    textPattern = Pattern.compile(simpleTextPatternText);
    nameMatcher = textPattern.matcher(inputFromField);
    System.out.println("Inside validation method!!");
    if (!nameMatcher.matches()) {
        System.out.println("NO MATCH!!!");
        FacesMessage msg = new FacesMessage(
                Your name cannot contain special characters);
        throw new ValidatorException(msg);
    }
}

As you see i manage to get inside the validation method when i try inputs that do contain special characters like: G#^r$>',... and i see the message that advice about it in the console, but i dont see the message on the displayed on the page, next to the input field. By the way: I also noticed that when i give empty input i dont see any validation messages and i did make it required="true". I checked the Error Log and also the console, but i dont find any Exceptions that can give me clues(Just the WARNING message that i copy pasted above). I am a begginer with JSF 2.0 It is the third day i am stuck with this problem, i dont know to solve it. Do i need to configure any XML? Do i need to implement any interface? Override any method?... Ill be very happy if someone could have a look at it and give me a hand. In advance thank you very much for your time reading.

like image 630
javing Avatar asked Mar 05 '11 11:03

javing


1 Answers

The <h:message> should not be nested inside <h:inputText>. Put them next to each other.

like image 173
BalusC Avatar answered Sep 28 '22 00:09

BalusC