Your concrete problem is caused because the upload action took place in a different HTTP request than the save action. You first choosed the file and then pressed Upload (request #1) and then the Save button (request #2), right? The network monitor in browser's builtin developer toolset (press F12) should also confirm it. The FacesContext#validationFailed()
is in essence request scoped, like the FacesContext
itself. So, when you set it during the request which invokes the upload action, then it's just "reset" during the request which invokes the save action.
This is indeed somewhat awkward. As the <p:fileUpload mode="advanced">
doesn't support Validator
s, as you already found out, there's not really a clean solution to this problem. You should be fiddling with a view scoped bean property to maintain the validation state across the requests on the same view.
private boolean validationFailed;
public void fileUploadListener(FileUploadEvent event) throws IOException {
// ...
validationFailed = !Utility.validateImageDimensions(bytes);
if (validationFailed) {
// Add message.
}
else {
// Process upload.
}
}
public void insert() {
if (validationFailed) {
// Add message.
}
else {
// Process insert.
}
}
By the way, I'd rather not set those messages as FATAL
, but as ERROR
. The enduser is namely capable of fixing it all by itself.