gpt4 book ai didi

validation - p :fileUpload required ="true" and custom validator doesn't work

转载 作者:行者123 更新时间:2023-12-03 16:42:18 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





PrimeFaces <p:fileUpload mode="advanced"> validator not fired

(2 个回答)


1年前关闭。




required <p:fileUpload> 的属性在 PrimeFaces 4.0 final 中似乎仍然不起作用,我尝试创建一个自定义验证器,如下所示。

@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> .
<p:fileUpload mode="advanced" 
required="true"
multiple="true"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
fileUploadListener="#{bean.fileUploadListener}">
<f:validator validatorId="fileUploadValidator"/>
</p:fileUpload>

但是从未调用过 validate 方法。因为我在 <p:dataGrid> 中显示图像,这种验证是非常需要的。有没有办法验证空 <p:fileUpload> ?

最佳答案

尝试这个

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

关于validation - p :fileUpload required ="true" and custom validator doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19925708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com