How to identify if any of a page's components failed validation after a submit

View: New views
3 Messages — Rating Filter:   Alert me  

How to identify if any of a page's components failed validation after a submit

by Louis Savoldy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After a page submit, I need to identify if any components on the page failed validation.  I’ve come up with two possible solutions, but they both seem to be workarounds and I'm not 100% they cover all conditions.

1. Traverse the page’s component hierarchy using a custom visitor.
        protected final boolean isFormComponentsValid() {
                final Object traversalValObj = visitChildren(new ValidFormComponentVisitor());
                if (traversalValObj == IVisitor.STOP_TRAVERSAL) {
                        return false;
                }

                return true;
        }

        public final class ValidFormComponentVisitor implements IVisitor {
                public Object component(final Component component) {
                        if (component instanceof FormComponent) {
                                if (!((FormComponent) component).isValid()) {
                                        return IVisitor.STOP_TRAVERSAL;
                                }
                        }
                        return IVisitor.CONTINUE_TRAVERSAL;
                }
        }

2. Traverse the feedback messages in search of an ERROR.
        public static final boolean isValidationError() {
                final Iterator<FeedbackMessage> messageIterator = Session.get().getFeedbackMessages().iterator();
                while (messageIterator.hasNext()) {
                        final FeedbackMessage feedbackMessage = messageIterator.next();
                        if (feedbackMessage.getLevel() == FeedbackMessage.ERROR) {
                                return true;
                        }
                }
                return false;
        }

Is there anything directly built into the framework that will provide me with this information?  Any other ideas will be appreciated.

Thanks!
Louis

Re: How to identify if any of a page's components failed validation after a submit

by Ned Collyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

er... form.hasError()?  form.onError()?

What do you mean by page? - are there lots of forms on the page all with errors?

Re: How to identify if any of a page's components failed validation after a submit

by Louis Savoldy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ned Collyer wrote:
er... form.hasError()?  form.onError()?

What do you mean by page? - are there lots of forms on the page all with errors?

Thanks Ned for pointing out the form’s hasError() and onError().  I could make use of these wtihin my visitor rather looking at all components.  But what if an error is created outside a form component?  Such as:
                if (!isPhoneDirectoryAvailble) {
                        Session.get().error("The phone directory is not available at this time…");
                }

Let me explain my need for this.  I’m writing a custom Refreshing view item reuse strategy that will redisplay the items as they were submitted if any page errors occur (such as validation).  I know there is already an existing ReuseIfModelsEqual strategy, but I cannot make use of it.  The reason is my model is many levels deep and is highly complex (hibernate generated) and has its own equals/hashcode strategy (our custom implementation) which will not work for this case and I don’t want to further override them.  My custom reuse strategy is rather simple.  If a page error occurred in which the page gets redisplayed, it simply makes use of the existing items (and does not try to make use of any new items like the ReuseIfModelsEqual strategy does).

So going back to my original question, what is the best way to determine if ANY error was generated on the page during the page submit?
LightInTheBox - Buy quality products at wholesale price