0

私は WAS 8.0.0.5 の MyFaces 2.0.4 と CDI を使用しています (CDI を失わずに WAS 8 のバージョンの JSF 2.0 を置き換えることはできません)。

コンポーネントに登録された 2 つのカスタム バリデータがあります。最初のものは、複数フィールドの比較を行います。2 番目のバリデーターは、CDI を使用して SSB を挿入し、SSB はエンティティーマネージャーを使用してデータベースを呼び出します。

1 番目のバリデータが意図的に失敗するような情報を入力すると、2 番目のバリデータが実行されます。なんで?最初の検証が失敗した場合、2 番目の検証を回避するにはどうすればよいですか? 1 つのバリデータが失敗すると、その後のすべてのバリデーションがバイパスされると思いました。

<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>

1 番目のバリデータ:

@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);
        }
    }
}

2 番目のバリデータ:

@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);
    }
}

}

4

1 に答える 1