p:fileUpload required =“true”,自定义验证器不起作用

时间:2013-11-12 09:44:11

标签: validation jsf primefaces jsf-2.2

由于required的{​​{1}}属性在PrimeFaces 4.0 final中似乎仍无效,我尝试按如下方式创建自定义验证程序。

<p:fileUpload>

并使用@FacesValidator(value="fileUploadValidator") public final class FileUploadValidator implements Validator { @Override public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException { System.out.println("fileUploadValidator called."); if(!(o instanceof UploadedFile)) { FacesMessage message = new FacesMessage(); message.setSeverity(FacesMessage.SEVERITY_ERROR); message.setSummary("Error"); message.setDetail("Required"); throw new ValidatorException(message); } } } 指定。

<p:fileUpload>

但是从未调用过validate方法。由于我在<p:fileUpload mode="advanced" required="true" multiple="true" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" fileUploadListener="#{bean.fileUploadListener}"> <f:validator validatorId="fileUploadValidator"/> </p:fileUpload> 中显示图像,因此非常需要此验证。有没有办法验证空<p:dataGrid>

1 个答案:

答案 0 :(得分:0)

试试这个

@ManagedBean(name = "docBean")
@ViewScoped
public class DocumentBean implements Serializable
{
  private UploadedFile file;

  public void handleFileUpload(FileUploadEvent event)
  {
     uploadedFile = event.getFile();
   }

   //action
   public void viewImage()
  {
    if(uploadFile==null){
     FacesContext saveContext = FacesContext.getCurrentInstance();
     saveContext.addMessage(null, new FacesMessage("Error", "Upload file  required"));
   }
 }
}