2011-11-24 100 views
1

我有一個try塊中的大文件大小的驗證,拋出該錯誤之後,它再次去檢查靜態約束的其他屬性的空值,並拋出該錯誤。如何擺脫雙重錯誤groovy

如何在返回第一個錯誤後停止流程?

這裏是大文件大小錯誤後的代碼

static constraints = { 
     applicationName(blank: false, size: 1..25) 
     applicationShortName(blank: false, size: 1..10) 
     applicationImage(nullable: false, maxSize: MAX_SIZE) 
     contentProviderId (
      validator: { 
       if (it == 0) { 
        return ['notSelected'] 
       } 
      } 
     ) 
     customErrorMessage (
       validator: { 
        if ("fileToBig".equals(it)) { 
         return ['fileToBig'] 
        } 
       } 
     ) 
    } 

try { 
      CommonsMultipartFile file = request.getFile('applicationImageUrl'); 

      logger.debug("POSTPROCESS: is file empty=${file.isEmpty()}") 
      if(!file.isEmpty()) { 

       try { 


         -- other logic 


       } 
       catch (Exception ex) { 
        logger.warn("Failed to upload file - improper file type", ex) 
        return []; 
       } 

       logger.debug("Getting new image file") 
       try { 
        -- logic 

        if (file.size <= MAX_SIZE) { 
         -- logic 
        } else { 
         customErrorMessage = "fileToBig"; (ERROR FOR BIG FILE SIZE) 


        } 
       } catch (Exception e) { 
        logger.warn("Failed to upload file", e) 
        customErrorMessage = "fileToBig"; 

       } 
      } else { 
       logger.debug("File was empty. Will check if there is a file in submission") 
       if (submission.applicationImage != null && submission.applicationImage != []) { 
        logger.debug("submission contains applicationImage=${submission.applicationImage}") 
        this.applicationImage = submission.applicationImage; 
       } 
      } 
     } catch (Exception e) { 
      this.errors.reject("error","An error occured when uploading file. Please try again."); 
      logger.error("Failed to upload file", e); 
      return []; 
     } 

     --logic 
     if (application != null) { //Application already exists! 
      submission.applicationId = application.id; 
      return [next: 10]; 
     } 

     return []; 
    } 

,應用圖像沒有被設置,所以它的投擲應用圖像零誤差,以及...

+0

我不知道有什麼辦法做到這一點。你能解釋爲什麼你想停止驗證,如果這個約束被違反?也許我可以提出另一種方法來實現你的目標。 –

+0

請檢查編輯的問題...請看看它 – Techie

回答

0

您可以添加自定義驗證程序添加到applicationImage字段,以便只在customErrorMessage字段不爲null時檢查空值。 這樣,如果customErrorMessage不爲null,則只會在驗證異常中獲取applicationImage錯誤。

在驗證關閉你正在檢查進入字段的值和整個對象,以及:

myField(validator: { val, obj -> return propertyName == "myField" }) 

因此,你可以做這樣的事情:

static constraints = { 
     applicationName(blank: false, size: 1..25) 
     applicationShortName(blank: false, size: 1..10) 
     applicationImage(validator: {val, obj -> 
      if (obj.customErrorMessage != null) { 
       if (val == null) return ['imageNull'] 
       if (val.size() > MAX_SIZE) return ['tooLarge'] 
      } 
     }) 
     contentProviderId (
      validator: { 
       if (it == 0) { 
        return ['notSelected'] 
       } 
      } 
     ) 
     customErrorMessage (
       validator: { 
        if ("fileToBig".equals(it)) { 
         return ['fileToBig'] 
        } 
       } 
     ) 
    } 

可能需要錯誤信息的調整,但我希望你明白我的觀點;)

+0

感謝您的提示,但我相信如果customErrorMessage爲null,那麼我們可以拋出applicationImage錯誤...我是否正確? – Techie

+0

如何在applicationImage自定義驗證器中訪問customErrorMesssage?我給這樣的事情.... applicationImage(MAXSIZE:MAX_SIZE, 驗證:{ 如果(customErrorMessage == NULL){ 回報[ '可爲空'] }} )和它說沒有這樣的屬性 – Techie

+0

我已編輯我的答案 – Krystian

0
applicationImage(maxSize: MAX_SIZE, 
      validator: {val, obj -> 
      if (obj.customErrorMessage == null) { 
       if (val == null) return ['nullable'] 

      } 
     })