1 Reply Latest reply on Jul 13, 2009 5:45 AM by ilya_shaikovsky

    rich:fileUpload adding validator

      Hi

      I have problem with adding validator to rich:fileUpload. I created my own validator, registered it in faces-config.xml, but this validator isnt called from rich:fileUpload component.

      I tried same validator (with small changes to work with String) from different component and validator was called.

      rich:fileUpload code:

      <rich:fileUpload id="userPhotoUpload" uploadData="#{userPhotoUploadController.files}" fileUploadListener="#{userPhotoUploadController.uploadListener}" acceptedTypes="jpg,jpeg" maxFilesQuantity="1" validator="#{sk.ku.validators.UserPhotoValidator}" listWidth="300px" listHeight="58px" required="true" />


      Validator code:

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package sk.ku.validators;

      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.InputStream;
      import javax.faces.application.FacesMessage;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.validator.Validator;
      import javax.faces.validator.ValidatorException;
      import org.ajax4jsf.resource.image.ImageInfo;

      /**
      *
      * @author swalko
      */
      public class UserPhoto implements Validator {

      private static final int MAX_IMAGE_SIZE = 128;
      private static final String IMAGE_FORMAT = "JPEG";
      private static final int REQUESTED_IMAGE_WIDTH = 300;
      private static final int REQUESTED_IMAGE_HEIGHT = 360;

      /**
      * Method for validating user photo parameters (size, format, width and height)
      * @param context
      * @param component
      * @param value
      */
      public void validate(FacesContext context, UIComponent component, Object value)
      throws ValidatorException {
      try {
      File sourceFile = (File) value;
      ImageInfo image = new ImageInfo();
      InputStream in = new FileInputStream(sourceFile);

      image.setInput(in);
      image.setDetermineImageNumber(true);
      image.setCollectComments(true);

      if (!image.check()) {
      FacesMessage message = new FacesMessage("Neznámy typ obrázka");
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
      }

      if (sourceFile.length() > MAX_IMAGE_SIZE * 1024) {
      FacesMessage message = new FacesMessage("Fotografia je vä�šia než " + MAX_IMAGE_SIZE + "kB");
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
      }

      if (!(image.getFormatName().equals(IMAGE_FORMAT))) {
      FacesMessage message = new FacesMessage("Fotografia nieje požadovaného formátu.");
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
      }

      if (image.getWidth() != REQUESTED_IMAGE_WIDTH || image.getHeight() != REQUESTED_IMAGE_HEIGHT) {
      FacesMessage message = new FacesMessage("Fotografia nemá rozmery " + REQUESTED_IMAGE_WIDTH + "x" + REQUESTED_IMAGE_HEIGHT + " bodov");
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
      }

      in.close();

      } catch (FileNotFoundException ex) {
      System.out.println(ex.getMessage() + " in the specified directory.");
      System.exit(0);
      } catch (IOException e) {
      System.out.println(e.getMessage());
      }
      }
      }