PrimeFaces

validator not fired

前端 未结 2 1764
误落风尘
误落风尘 2020-11-27 20:28

since fileLimit doesn\'t exist in primefaces 3.4 anymore I\'m trying a work around implementing a validator, the problem is that the method validate is never invoked. That\'

2条回答
  •  情深已故
    2020-11-27 21:13

    For validating a required primefaces file upload in mode advanced (ajax) it is possible to use this:

    
        
    
    

    Where the implementation of the bean.processValidations() method would be something along the lines of:

    public void processValidations() {
            FacesContext context = FacesContext.getCurrentInstance();
            UIInput fileUploadComponent = fileUploadsBean.getFileUploadComponent();
            if (fileUploadComponent!=null && !isFileUploaded()) {
                fileUploadComponent.setValid(false);
                context.addMessage(fileUploadComponent.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR, messageSummary, messageDetail));
                context.validationFailed();
            }
        }
    

    Where fileUploadsBean would be a REQUEST scoped CDI bean (won't work with standard JSF ManagedBeans) which you inject to your bean which has the processValidations() method defined, the method fileUploadsBean.getFileUploadComponent() returns the primefaces file upload component (you will use for that). The method isFileUploaded() will determine if the file has been uploaded or not (probably just a null check on a member variable which you fill from fileUploadListener).

    If you want to highlight the file upload button you can of course conditionally add a styleClass which you can then use for adding a red border for example.

    styleClass="#{fileUploadsBean.fileUploadComponent.valid ? '' : 'validationFailed'}"
    

    As a result the validation failed message for the primefaces file upload will be displayed along with all other jsf validation messages. You might have problem with maintaining order of the validation messages (will be always at the end), but it still beats displaying the failed upload file validation after user dealt with all the standard jsf validation messages from different fields and your action in a backing bean has been finally reached.

提交回复
热议问题