Daniela Mogini
Daniela Mogini

Reputation: 299

PrimeFaces uploader: how to mark this component as required?

in my application, I have a form with multiple primefaces fileUploader in simple mode. These uploader components are dynamically generated, one for each row of a dataTable.
I'd like to mark them as required so I tried this:

<h:form enctype="multipart/form-data" id="aform">
   <p:dialog id="dlgId">
    <p:dataTable var="foo" id="table"
        value="#{myBean.fooList}">
        <p:column>
           <p:fileUpload value="#{foo.file}" mode="simple" required="true"/>
        </p:column>
    </p:dataTable>

    <p:commandButton id="submit" value="Submit" ajax="false"
        actionListener="#{myBean.listener}" update="dlgId">
    </p:commandButton>
  </p:dialog>
</h:form>

The problem is that the form is correctly working if I upload all the files but the validation is not executed. The PrimeFaces version is 3.4.

Upvotes: 0

Views: 107

Answers (1)

BalusC
BalusC

Reputation: 1108632

As per the comments, you have actually this form inside a dialog which get closed/hidden after submit. You basically need to let the visible attribute of the dialog evaluate true when the form inside the dialog is submitted and there's a validation error.

You can achieve it as follows:

<p:dialog ... visible="#{dialogForm.submitted and facesContext.validationFailed}">
    <h:form binding="#{dialogForm}">
        ...
    </h:form>
</p:dialog>

Unrelated to the concrete problem, the dialog should not go inside a form, instead the dialog should have its own form (because 1) the dialog may be implicitly relocated to the end of body by JS and 2) you don't want to submit/process all other inputs which are outside the dialog's view). So if the dialog is currently by itself inside a form with all other input controls, you should move it outside, e.g. to the very bottom of the body.

Upvotes: 1

Related Questions