PrimeFaces validator not fired

According to the FileUpload and FileUploadRenderer source code, the validator is only invoked when mode="simple" is been used (note: this in turn requires ajax="false" on command). The advanced mode will namely not set the uploaded file as component’s submitted value, causing it to remain null until the listener method is invoked. As long as the submitted value is null, the validators are not invoked.

I’m not sure if this is intentional. Theoretically, it should be possible to set UploadedFile as submitted value and have the validator to rely on it. You might want to create an enhancement report at PrimeFaces issue tracker.

In the meanwhile, in spite of it being a poor practice, your best bet is really performing the validation in fileUploadListener method. You can just trigger validation failure add faces messages through the FacesContext like follows:

if (fail) {
    context.validationFailed();
    context.addMessage(event.getComponent().getClientId(context), new FacesMessage(
        FacesMessage.SEVERITY_ERROR, messageSummary, messageDetail));
}

Otherwise, you’d need to create a custom renderer for the <p:fileUpload> which sets the submitted value during the decode() (I however don’t guarantee that it would work in practice, you’ll maybe stumble upon a peculiar problem which may turn out to be the reason why PrimeFaces didn’t initially implement it like that).

By the way, your first and second validator attempt are correct. The third attempt works only if you used @ManagedBean instead of @FacesValidator (which is often done when injection of an @EJB is mandatory — which isn’t possible in a @FacesValidator). The fourth attempt is invalid.

Leave a Comment