2012-12-22 25 views
1

我有一個上傳字段。我只想上傳圖片。我已經應用相同的驗證。但出錯時,我想將該錯誤添加到窗體上的feedbackpanel。Wicket自定義錯誤消息

UploadPanel.html

<form wicket:id="frmProduct" > 
     <div wicket:id="feedback"></div> 
     <label>Category1 *:</label><br/> 
     <input type="text" wicket:id="Category1"><br/> 
     <label>Category2 *:</label><br/> 
     <input type="text" wicket:id="Category2"><br/> 
     <label>ProductName *:</label><br/> 
     <input type="text" wicket:id="ProductName"><br/> 
     <label>TaxAmount *:</label><br/> 
     <input type="text" wicket:id="TaxAmount"><br/> 
     <label>UnitPrice *:</label><br/> 
     <input type="text" wicket:id="UnitPrice"/><br/> 
     <label>Description</label><br/> 
     <textarea wicket:id="Description" id ="Description" rows="6" cols="20"></textarea><br/> 
     <label>Description</label><br/> 
     <input wicket:id="uploadField" size="40" type="file"/><br/> 
     <input type="submit" wicket:id="submit" value="Save"/> 
</form> 

UploadPanel.java

    if(uploadField.getFileUpload() != null && uploadField.getFileUpload().getClientFileName() != null){ 
        FileUpload upload = uploadField.getFileUpload(); 
        String ct = upload.getContentType(); 

        if (!imgctypes.containsKey(ct)) { 
         hasError = true; 
        } 

        if(upload.getSize() > maximagesize){ 
         hasError = true; 
        } 

        if(hasError == false){ 
         System.out.println("######################## Image can be uploaded ################"); 
         imageEntry.setContentType(upload.getContentType()); 
         imageEntry.setImageName(upload.getClientFileName()); 
         imageEntry.setImageSize(upload.getSize()); 
         if(imageEntry != null){ 
          try { 
           save(imageEntry,upload.getInputStream()); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }else{ 
         target.appendJavaScript("$().toastmessage('showNoticeToast','Please select a valid image!!')"); 
         System.out.println("#################### Error in image uploading ###################"); 
        } 
       }else{ 
        System.out.println("########################### Image not Selected #####################"); 
       } 

任何幫助和建議表示讚賞!提前致謝。

回答

2

FileUploadField是一個表單組件,所以你可以簡單地做一個

fileUploadField.error("error with file upload"); 

PS:不是

if(hasError == false) 

if (!hasError) 

其更好:)

+0

謝謝Istvan。你是絕對正確的。快樂編碼:) –