1

I'm trying to put validation in my <p:fileUpload> . when the user upload without putting any file he get's an error message. I'm using mode="simple" and required="true" but required="true" doesn't work.

P.S: I need to use mode="simple" because I need <p:commandButton> to submit other data.

<p:panelGrid columns="2">
    <h:outputLabel id="image" value="Select Image: *" />

    <p:fileUpload value="#{Jcalendar.file}" mode="simple"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  required="true"
                  requiredMessage="File not selected !!"/>

    <f:facet name="footer">
        <p:commandButton value="Submit"
                         ajax="false"
                         action="#{Jcalendar.Upload}"
                         update=":form:msgs" />
    </f:facet>
</p:panelGrid>
Joe
  • 7,749
  • 19
  • 60
  • 110
Moodi1409
  • 155
  • 1
  • 10
  • No validators including your own custom validators can work with ``. – Tiny Dec 23 '14 at 21:36
  • @Tiny can you show me how ? i'm new to primefaces – Moodi1409 Dec 23 '14 at 21:38
  • Nothing other than putting the validation logic into the listener (or sonewhere maybe) of `` can be done (unless some tedious hacks / home-grown rederers are made). [See for example](http://stackoverflow.com/q/13865136/1391249) (and [maybe](http://stackoverflow.com/q/22659268/1391249)). – Tiny Dec 23 '14 at 21:41
  • Forgot to consider that you are using `mode="simple"`. You may have chances to implement a custom validator (I never used `mode="simple"`). – Tiny Dec 23 '14 at 21:48
  • @Tiny they all use mode="advanced" but i can not use it because I don't want to use (FileUploadEvent event) – Moodi1409 Dec 23 '14 at 21:49
  • Since I have not yet used `mode="simple"`, I can only hope that a custom validator should work for you using `mode="simple"` (using `mode="advanced"`, it does not work. The `validate()` will not be invoked). Try implementing it and see, if the `validate()` method is invoked or not. If it does then, you can put any logic of your interest in that method afterwards. – Tiny Dec 23 '14 at 21:55

1 Answers1

0

As per the 5.2 source code, this will indeed fail when Servlet 3.0 native upload is being used instead of Apache Commons FileUpload.

The NativeFileUploadDecoder#decodeSimple() will only set the submitted value to an empty string when the input was not submitted at all. This will thus fail if the input is actually submitted, but with an empty value.

51        if(part != null) {
52            fileUpload.setTransient(true);
53            fileUpload.setSubmittedValue(new NativeUploadedFile(part));
54        }
55        else {
56            fileUpload.setSubmittedValue("");
57        }

The CommonsFileUploadDecoder#decodeSimple() will set the submitted value to an empty string when the submitted file name is empty, exactly as intented.

54        if(file != null) {
55            if(file.getName().equals("")) {
56                fileUpload.setSubmittedValue("");
57            } else {
58                fileUpload.setTransient(true);
59                fileUpload.setSubmittedValue(new DefaultUploadedFile(file));
60            }
61        }  

Your best bet to get it to work is to report an issue to PrimeFaces guys and in the meanwhile either customize/patch the renderer, or switch back to Commons uploader.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555