code_fish
code_fish

Reputation: 3436

Wicket Custom error message

I have a upload field. I want to upload only images. I have applied validation for the same. But on error I want the error should be added to feedbackpanel on the form.

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 #####################");
                }

Any help and advices appreciated! Thanks in advance.

Upvotes: 0

Views: 1670

Answers (1)

Istvan Devai
Istvan Devai

Reputation: 4022

The FileUploadField is a form component, so you can simply do a

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

PS: instead of

if(hasError == false)

do a

if (!hasError)

its nicer :)

Upvotes: 2

Related Questions