4 Replies Latest reply on Sep 27, 2007 7:59 AM by Bieth Bruno

    Validate fileUpload

    Bieth Bruno Newbie

      Just wondering would it be possible to validate (using the seam validation mecanism) a file.
      I would like to upload an image but the accept attribute of the fileUpload component is neglected by the browser. So I wrote an Image validator and I would like to use it with s:validate. Problem fileUpload is not a EditableValueHolder. Thus I have to manually validate it (using ClassValidator) ...

      Thanks,
      Bruno

        • 1. Re: Validate fileUpload
          Pete Muir Master

          fileUpload is a EditableValueHolder in 1.3.0.ALPHA and 2.0.BETA

          • 3. Re: Validate fileUpload
            Bieth Bruno Newbie

            Hello,

            fileUpload is a EditableValueHolder true and validate all attach a modelvalidator to it.
            But the file upload occur during the update model phase so the data is not validated.

            Cheers,
            Bruno

            • 4. Re: Validate fileUpload
              Bieth Bruno Newbie

              I've submitted a patch for UIFileUpload.
              http://jira.jboss.com/jira/browse/JBSEAM-1981

              With this patch you can use the hibernate validator framework.
              For example I've done an Image annotation which check if a byte[] can be read by ImageIO.

              @ValidatorClass(ImageValidator.class)
              @Target({ElementType.FIELD, ElementType.METHOD})
              @Retention(RetentionPolicy.RUNTIME)
              @Documented
              public @interface Image {
               String message() default "not a valid image";
              }
              


              public class ImageValidator implements Validator<Image> {
              
               public void initialize(Image parameters) {
               }
              
               public boolean isValid(Object value) {
               if( value == null ) return true;
               if( ! (value instanceof byte[]) ) return false;
              
               byte[] data = (byte[])value;
              
               try {
               BufferedImage img =
               ImageIO.read( new ByteArrayInputStream( data ) );
              
               return img == null ? false : true;
               } catch (IOException e) {
               return false;
               }
               }
              }
              


              So you can use it in your Entity
              @Entity
              public class AnEntity {
               @Lob
               @Image
               private byte[] data;
              
               ...
              }
              


              In the Facelets :
              <s:fileUpload id="uplEditedImage" value="#{image.data}"/>
              


              About my last post, I'm not quite right :) file upload occur during the apply request value phase but the submittedValue attribute wasn't set and the ModelValidator need a "value" attribute instead of "data".