Does any body know how to implement validation with validation message to uploadFile in PrimeFaces?
View:
<p:fileUpload id="upload"
fileUploadListener="#{fileBean.handleFileUpload}"
update="uploads" auto="true" multiple="true" skinSimple="true">
<f:validator validatorId="uploadValidator"/>
<p> <h:messages id="messages" /></p>
</p:fileUpload>
FileBean:
List<UploadedFile> uploadedFiles;
public void handleFileUpload(FileUploadEvent event) {
if (uploadedFiles == null) {
uploadedFiles = new ArrayList<>();
}
uploadedFiles.add(event.getFile());
}
uploadValidator.java
@FacesValidator("uploadValidator")
public class UploadValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Part file = (Part) value;
FacesMessage message=null;
try {
if (!file.getName().matches("\\w+"))
message=new FacesMessage("Wrong file name");
if (message!=null && !message.getDetail().isEmpty())
{
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
} catch (Exception ex) {
throw new ValidatorException(new FacesMessage(ex.getMessage()));
}
}
}
I need to check if uploaded file name is on Latin Unicode and if not - show user a message "Wrong file name." My code doesn't work. No message is displayed no matter the file name.
Thank you.