4

I have a JSF form which contains a Primefaces fileupload component.

The required attribute of the fileupload component is not working; When I hit the form's send button without file attachments, the form is sent and no error message is displayed.

This is the simplified form code I am using for testing:

<h:form id="NewRegistryForm">
  <fieldset>
    <p:outputLabel for="address" value="Address" />
    <h:inputText 
        name="address" 
        value="#{registryBean.address}" 
        required="true" 
        requiredMessage="Address field is required" />
  </fieldset>
  <fieldset>
    <p>File attachments:</p>
    <p:fileUpload 
      fileUploadListener="#{registryBean.handleFileUpload}" 
      mode="advanced" 
      label="Choose file"
      multiple="true" 
      update="registryFileList"
      invalidFileMessage="Invalid file type"
      sizeLimit="512000" 
      fileLimit="15" 
      allowTypes="/(\.|\/)(jpg|png|gif|pdf|odt|doc|docx)$/i" 
      onstart="setSubmitButtonEnabled(false)" 
      oncomplete="setSubmitButtonEnabled(true)" 
      onerror="setSubmitButtonEnabled(true)" 
      auto="true" 
      required="true"
      requiredMessage="A file attachment is required." />
  </fieldset>
  <p:dataList id="registryFileList" value="#{registryBean.uploadedFiles}" 
  var="file" emptyMessage="" >#{file.file.fileName}, #{file.file.size}</p:dataList>
  <p:commandButton 
    type="submit" 
    action="#{registryBean.sendRequest}" 
    value="Send form" 
    validateClient="true" />
</h:form>

The required text input field shows an error message when the field is not filled, but the fileupload component produces no message when no file is attached. What am I doing wrong?

NotGaeL
  • 8,344
  • 5
  • 40
  • 70
  • 2
    It is not supported in ``. Do validation in the associated managed bean instead avoiding any validator. – Tiny May 20 '15 at 09:13
  • 1
    According to the [docs](http://www.primefaces.org/docs/guide/primefaces_user_guide_5_2.pdf) it should be supported (p. 211). Assuming OP is using PrimeFaces 5.2 – LarsBauer May 20 '15 at 14:36
  • Yes I am. I read the docs too, nothing about not being supported but it is not working... I have already implemented the validation suggested by @Tiny and will assume it is not unless somebody tells me they got it to work. – NotGaeL May 20 '15 at 15:15
  • 3
    Is this acceptable as dupe? http://stackoverflow.com/questions/13865136/primefaces-3-4-fileupload-validator-not-fired/13868094#13868094 As to docs, they are generated based on component attributes. FileUpload component class extends from UIInput component class which has a `required` attribute. But this is indeed not further used in advanced mode. I do however agree that PrimeFaces should have clarified this more in the docs. – BalusC May 20 '15 at 19:10
  • The answer seems to apply, but the question is different enough to deserve its place in my opinion (at least I oversaw it on my search before asking). The ideal question would ask about both things. – NotGaeL May 20 '15 at 19:38
  • I understand that the question is different, but I would basically copypaste the answer and only add a remark about the misleading documentation (or just update the existing answer :) ). And, I like DRY. – BalusC May 20 '15 at 21:49

0 Answers0