David Sonnenfeld
David Sonnenfeld

Reputation: 698

Handle feedback-Messages with a custom validation framework/object in apache wicket

I am using my own validation, but the view of the form (e.g. if a component is invalid) should be highlighted or customized within wicket...

So e.g. I have my Wicket Form

<form wicket:id="inputForm">
         <p>
             <label for="Recipient">Recipient</label>
             <input wicket:id="Recipient" id="Recipient" type="text" size="40"/>
         </p>
         <p>
             <label for="Details">Details</label>
             <input wicket:id="Details" id="Details" type="text" size="40"/>
         </p>
</form>

Then I have my Form.java: Here I set the values of my Java Object "Invoice" and use onValidateModelObjects to override the wicket validation and use my own validation... This method just calls a simple own created ValidatorObject that takes care of the validation...

public InvoiceForm() {
    ...

    Form<Invoice> form = new Form("inputForm", formModel) {
        /*
         * Validation takes place here
         */
        @Override
        public void onValidateModelObjects() {
            ValidationProcess vp = new ValidationProcess();
            vp.validate("Rules.js", r);
            msg = "";
            if (vp.getConstraintViolations().size() > 0) {

                for (String s : vp.getConstraintViolations()) {
                    msg = msg + s;
                }
            } else {
                msg = "no errors.";
            }
        }
    };

    //adding Textfields to the Form etc...

...
}

What I need: Above you can see that my errorMessages will be saved into the msg String! Now what I want is Wicket to take this String and apply it to the Wicket FeedBackMessage System or however...

  1. I need to get the component that will be rendered at the moment...
  2. I have to set the error Message of this component to my component...

how can I do that?

I guess I need to override some methods, but I dont know which ones and where...

Upvotes: 0

Views: 1781

Answers (2)

David Sonnenfeld
David Sonnenfeld

Reputation: 698

I got it working with the following Code:

...
//Textfields or Form Components GO HERE...

        @Override
        public void onValidateModelObjects() {
            ValidationProcess vp = new ValidationProcess();
            vp.validate("Rules.js", r);
            msg = "";
            if (vp.getConstraintViolations().size() > 0) {

                for (String s : vp.getConstraintViolations()) {
                    if (s.contains(";")) {
                        String[] splitted = s.split(";");
                        String elementname = splitted[0];
                        String errorMessage = splitted[1];

                        if (elementname.equals("price")) {
                            //SET Error of Component 
                            price.error(elementname + " " + errorMessage + ". ");
                          } else if (elementname.equals("recipient")) {
                            recipient.error(elementname + " " + errorMessage);
                        }
                    }
                }
            }
        }

//Rest of the Code

Upvotes: 0

divanov
divanov

Reputation: 6339

There is a special component, FeedbackPanel, which is showing validation and submission errors on the page. So add FeedbackPanel to the page with the form.

Then Form.error() method registers an error feedback message for this component to be shown on FeedbackPanel. For example,

    @Override
    public void onValidateModelObjects() {
        ValidationProcess vp = new ValidationProcess();
        vp.validate("Rules.js", r);
        if (vp.getConstraintViolations().size() > 0) {
            msg = "";
            for (String s : vp.getConstraintViolations()) {
                msg = msg + s;
            }
        } else {
            msg = "no errors.";
        }
        error(msg, null);
    }

Also note, that you can define and attach an implementation of IFormValidator to a form with Form.add() method to perform validation for you.

Upvotes: 1

Related Questions