Lock send button when no file has been uploaded

Asked

Viewed 46 times

0

So, guys, I have a fileUpload component from primefaces for uploading files. It turns out that even with the required="true", if the user click submit without having uploaded any file the system sends.

 <p:fileUpload
     required="true"
     requiredMessage="Comprovante ou declaração de vínculo de trabalho: item obrigatório"
     fileUploadListener="#{comprovanteBean.uploadArquivoDespesaTransporte}" update="@form" 
     label="Arquivo" uploadLabel="Enviar" cancelLabel="Cancelar" 
     allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
     invalidFileMessage="São permitidos apenas arquivos do tipo: jpeg, jpg, png, pdf)"
     sizeLimit="1048576"invalidSizeMessage="O tamanho máximo permitido é de 1 MB." 
     fileLimit="1">
</p:fileUpload>



<p:commandButton value="Enviar comprovantes" update="@form" ajax="false" action="#{comprovanteBean.finalizarInscricao()}">
    <f:param name="inscricaoFinalizada" value="true"/>
</p:commandButton> 

On the site of the first faces their button is locked, but not active when I upload, then I could not get their code: https://www.primefaces.org/showcase/ui/file/upload/basic.xhtml

Does anyone have any idea how to not allow the user to register without sending this file??

1 answer

0

You can try enabling the upload button only if there is a successful file upload. I think the simplest is to use a flag, initially set as true:

In XHTML:

<p:commandButton value="Enviar comprovantes" update="@form" ajax="false" action="#{comprovanteBean.finalizarInscricao()}" disabled="#{comprovanteBean.semArquivos}">
    <f:param name="inscricaoFinalizada" value="true"/>
</p:commandButton>

No Bean:

private boolean semArquivos = true;

public boolean isSemArquivos() {
    return this.semArquivos;
}

public boolean setSemArquivos(boolean semArquivos) {
    this.semArquivos = semArquivos;
}

public void uploadArquivoDespesaTransporte(FileUploadEvent event) {
    //Executar o seu método já existente
    setSemArquivos(getArquivos().isEmpty());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.