Created
January 25, 2012 20:04
-
-
Save anonymous/1678345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2012 Michal Wegrzyn <bend-up@users.sourceforge.net> | |
* Licensed under The MIT license. | |
* You can use this file as long as you retain the above copyright notice. | |
*/ | |
import org.apache.wicket.AttributeModifier; | |
import org.apache.wicket.Component; | |
import org.apache.wicket.ajax.AjaxRequestTarget; | |
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior; | |
import org.apache.wicket.feedback.IFeedback; | |
import org.apache.wicket.markup.html.form.Form; | |
import org.apache.wicket.markup.html.form.FormComponent; | |
import org.apache.wicket.util.time.Duration; | |
import org.apache.wicket.util.visit.IVisit; | |
import org.apache.wicket.util.visit.IVisitor; | |
/** | |
* Behaviour which automatically refreshes feedback panels, | |
* checks whether form components are valid and marks them | |
* with appropriate css class. | |
* | |
* @author Michal Wegrzyn | |
* | |
*/ | |
public class FormAutoValidatingBehavior extends AjaxFormSubmitBehavior { | |
private static final long serialVersionUID = 1L; | |
private static final Duration VALIDATION_THROTTLE = Duration.NONE; | |
public FormAutoValidatingBehavior(Form<?> form ) { | |
this( form, "onchange", VALIDATION_THROTTLE ); | |
} | |
public FormAutoValidatingBehavior(Form<?> form, String event, Duration throttleDelay ) { | |
super( form, event ); | |
if ( throttleDelay != null ) { | |
setThrottleDelay( throttleDelay ); | |
} | |
} | |
/** | |
* Adds this behavior to all form components of the specified form | |
* | |
* @param form | |
* @param event | |
* @param throttleDelay | |
*/ | |
public static void addToAllFormComponents(final Form<?> form, final String event, | |
final Duration throttleDelay, final boolean recurse) { | |
form.visitChildren(FormComponent.class, new IVisitor<Component, Void>() { | |
public void component( final Component component, final IVisit<Void> visit ) { | |
FormAutoValidatingBehavior behavior = new FormAutoValidatingBehavior( form, event, throttleDelay ); | |
component.add( behavior ); | |
if ( !recurse ) { | |
visit.dontGoDeeper(); | |
} | |
} | |
}); | |
} | |
@Override | |
protected void onSubmit( AjaxRequestTarget target ) { | |
addVisibleFeedbacks( target ); | |
onUpdate( target, isFormValid() ); | |
markFields( target ); | |
} | |
@Override | |
protected void onError(AjaxRequestTarget target) { | |
addVisibleFeedbacks( target ); | |
onUpdate( target, isFormValid() ); | |
markFields( target ); | |
} | |
/** | |
* Action executed during update on field. | |
* @param target | |
* @param isFormValid Indicates whether all form component are valid | |
*/ | |
protected void onUpdate( AjaxRequestTarget target, boolean isFormValid ) { | |
} | |
/** | |
* Performs check on all form's fields and marks every with appropriate css class. | |
* @param target | |
*/ | |
protected void markFields( final AjaxRequestTarget target ) { | |
getForm().visitChildren(FormComponent.class, new IVisitor<Component, Void>() { | |
public void component( final Component component, final IVisit<Void> visit ) { | |
FormComponent<?> formComponent = (FormComponent<?>) component; | |
if ( component.isVisibleInHierarchy() ) { | |
component.add( new AttributeModifier( "class", "component-" + ( formComponent.isValid() ? "valid" : "invalid" ) ) ); | |
target.add( component ); | |
} | |
} | |
}); | |
} | |
/** | |
* Performs check on all form's fields whether are they all valid. | |
*/ | |
private boolean isFormValid() { | |
Boolean result = getForm().visitChildren(FormComponent.class, new IVisitor<Component, Boolean>() { | |
public void component( final Component component, final IVisit<Boolean> visit ) { | |
FormComponent<?> formComponent = (FormComponent<?>) component; | |
if ( !formComponent.isValid() ) { | |
visit.stop( Boolean.FALSE ); | |
} | |
} | |
}); | |
return result == null || result; | |
} | |
/** | |
* Refreshes visible feedback components. | |
* @param target | |
*/ | |
private void addVisibleFeedbacks( final AjaxRequestTarget target ) { | |
getComponent().getPage().visitChildren( IFeedback.class, new IVisitor<Component, Void>() { | |
@Override | |
public void component( Component component, IVisit<Void> visit ) { | |
if ( component.isVisibleInHierarchy() ) { | |
target.add( component ); | |
} | |
} | |
} ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment