cocorossello
cocorossello

Reputation: 1357

o:inputFile optional and accept validation

I would like to add an optional "attach screenshot" field to my form, so I'm using the o:inputFile component:

<o:inputFile id="file" required="false" maxsize="1048576"  accept="image/png,image/gif" />

If I leave the field blank I get an error: Attach screenshot: Media type of file '' does not match 'image/png,image/gif'

How can I make that validation optional?

Upvotes: 1

Views: 631

Answers (1)

BalusC
BalusC

Reputation: 1109222

This is caused by unexpected behavior in MyFaces. It works fine in Mojarra which gives null when no file is selected. MyFaces, however, gives a non-null Part instance with an empty file name and a size of 0. The content type of that empty Part defaults to application/octet-stream and therefore never matches the specified accept attribute.

For now, your best bet is to make the accept attribute conditional based on the current phase ID and the file size. We want it to always evaluate during render response phase, and during other phases only when the file size is more than 0, otherwise default the accept to */*.

accept="#{facesContext.currentPhaseId.ordinal eq 6 
          or component.submittedValue.size gt 0 
              ? 'image/png,image/gif' 
              : '*/*'}"

I will fix this for upcoming OmniFaces 2.6 as per issue 315.

Upvotes: 1

Related Questions