1

hi i wrote a custom a validator which gets the system name and compare it against the id in database, now i wanna apply a check if this value is exactly the same, user must be allowed to click the button and move on else some error message should be displayed. and i am really confused how to call the validator() on through ajax.

my view page code is

<h:commandButton action="sample?faces-redirect=true" value="submit">
                    <f:ajax execute="#{csample.UserValidator}" render="@form" >
                    <h:inputText name="idtext" value="#{csampleBean.id}" />
                    </f:ajax>
                </h:commandButton>

and my custom validator

public void UserValidator(FacesContext context, UIComponent toValidate, Object value)
            throws UnknownHostException, ValidatorException, SQLException, NamingException
    {
        java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
        String machine=  localMachine.getHostName();

        String query = "select * from USER_ where USER_ID = '"+machine+"'";

        Context initContext = new InitialContext();
        Context envContext = (Context)initContext.lookup("java:/comp/env");
         DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
        Connection conn = ds.getConnection();   
        Statement stat = conn.createStatement();
        //get customer data from database
        ResultSet result =  stat.executeQuery(query);

        if (query==machine)
                // what to do here      
            conn.close();

need some guidance

lost
  • 165
  • 1
  • 4
  • 10

1 Answers1

1

You need to create a class implementing the Validator interface. On validation fail, just throw a ValidatorException with a FacesMessage. JSF will then take care that the FacesMessage ends up in the right <h:message> associated with the input component.

You can register the custom validator to JSF by annotating it with @FacesValidator with therein the validator ID. You can reference it in <h:inputXxx validator> or <f:validator validatorId>.

Here's a kickoff example:

@FacesValidator("userValidator")
public class UserValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // ...

        if (!valid) {
            String message = "Sorry, validation has failed because [...]. Please try again.";
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
        }
    }

}

Which is been used as follows (note: <h:inputText> does not have name attribute! instead use id; also note that your initial code snippet has some nesting which isn't making any sense):

<h:inputText id="idtext" value="#{csampleBean.id}" validator="userValidator">
    <f:ajax render="idtextMessage" />
</h:inputText>
<h:message id="idtextMessage" for="idtext" />
<h:commandButton action="sample?faces-redirect=true" value="submit" />

See also:


Unrelated to the concrete problem, your JDBC code is leaking DB resources. Please fix that as well.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555