我正在使用 WAS 8.0.0.5 的 MyFaces 2.0.4 AND CDI(不能在不丢失 CDI 的情况下替换 WAS 8 的 JSF 2.0 版本)。
我有 2 个自定义验证器注册到一个组件。第一个进行多场比较。第二个验证器使用 CDI 注入 SSB,它使用实体管理器调用数据库。
如果我输入导致第一个验证器故意失败的信息,第二个验证器仍然会执行。为什么?如果第一次验证失败,如何避免第二次验证?我认为如果一个验证器失败,那么所有后续验证都会被绕过。
<h:form id="registrationForm">
<fieldset>
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<legend>Register</legend>
<div class="form-row">
<h:outputLabel for="userId" value="*User Id"/>
<h:inputText id="userId" value="#{registration.userId}" required="true" size="20">
<f:validator validatorId="userIdPasswordValidator" />
<f:attribute name="passwordComponent" value="#{passwordComponent}"/>
<f:validator binding="#{duplicateUserValidator}" /> <-- uses CDI
</h:inputText>
</div>
<div class="form-row">
<h:outputLabel for="password" value="*Password"/>
<h:inputSecret id="password" type="password" binding="#{passwordComponent}" value="#{registration.password}" required="true">
</h:inputSecret>
</div>
<div class="form-row">
<h:outputLabel for="confirmPassword" value="*Confirm Password"/>
<h:inputSecret id="confirmPassword" type="password" value="#{registration.confirmPassword}" required="true" />
</div>
<div class="form-row">
<h:commandButton styleClass="btn btn-warning" value="Register" type="submit" action="#{registration.register}" />
</div>
</fieldset>
</h:form>
第一个验证者:
@FacesValidator("userIdPasswordValidator")
public class UserIdPasswordValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String userId = (String)value;
UIInput passwordInput = (UIInput)component.getAttributes().get("passwordComponent");
String password = (String) passwordInput.getSubmittedValue();
if (userId.equals(password)) {
FacesMessage message = new FacesMessage(null, "The Password cannot be the same as your User ID.");
throw new ValidatorException(message);
}
}
}
第二个验证器:
@Named
@RequestScoped
public class DuplicateUserValidator implements Validator {
@Inject
UserService userService;
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (userService.getUser(value.toString()) != null) {
FacesMessage message = new FacesMessage(null, "This User ID is already registered. Please logon or choose another one to register.");
throw new ValidatorException(message);
}
}
}