开发者

ManageBean method don't works anymore because of FacesValidator?

I was having some problem with password validation, which @BalusC help me out, here. But aft开发者_开发百科er I insert the validation code in my form, when I call the controller method, nothing happens. Here goes my JSF page: Register.xhtml

<!DOCTYPE html>
<html lang="pt-br"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <title>TITLE</title>
    </h:head>

    <h:body>
         <h:form id="form">
            <h:panelGrid columns="3" >

                <h:outputLabel for="name"   value="Nome:" />
                <h:inputText   id="name"    value="#{register.person.name}"  >  
                    <f:ajax    event="blur" listener="#{register.validateName}" render="m_name" />
                </h:inputText>
                <rich:message  id="m_name"  for="name"  ajaxRendered="false"/>

                // some fields ..                    

                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{register.user.password}">
                    <f:validator validatorId="confirmPasswordValidator" />
                    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
                    <f:ajax event="blur" execute="password confirm" render="m_password" />
                </h:inputSecret>
                <rich:message id="m_password" for="password" />

                <h:outputLabel for="confirm" value="Senha (novamente):" />
                <h:inputSecret id="confirm" binding="#{confirmPassword}" >
                    <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" />
                </h:inputSecret>
                <rich:message id="m_confirm" for="confirm" />

                <h:commandButton value="Register" action="#{register.registerPerson}" >
                    <f:ajax execute="@form" render="@form" />
                </h:commandButton>

                <h:outputText value="#{register.recordStatus}" id="out"  />
                <a4j:status>
                    <f:facet name="start">
                        <h:graphicImage name="loader.gif" library="image"/>
                    </f:facet>
                </a4j:status>    
        </h:panelGrid>
    </h:form>
    </h:body>
</html>

So when I fill the form and try to register, nothing happens, not even any exception it's lauched.

The passwod validator (thanks BalusC mate):

@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String password = (String) value;
        String confirm = (String) component.getAttributes().get("confirm");

        if (password == null || confirm == null) {
            return; // Just ignore and let required="true" do its job.
        }

        if (!password.equals(confirm)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
        }else{
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
        }
    }

}

I think this problem is very weird, I really don't know what's happening here. Thanks guys.


You threw a ValidatorException on a succesful match.

if (!password.equals(confirm)) {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
} else {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
}

This causes JSF to block the form submit. The severity doesn't matter. A validator exception is a validator exception. It indicates a vaildation failure.

Rather use FacesContext#addMessage() instead.

if (!password.equals(confirm)) {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
} else {
    context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜