Hi!
AjaxFormComponentUpdatingBehavior does not submit the other form
values, only the involved component. If you want to handle other form
values you will need something like AjaxFormSubmitBehavior or
AjaxFormSubmittingChangeListenerBehavior:
public abstract class AjaxFormSubmittingChangeListenerBehavior extends
AjaxFormSubmitBehavior {
private final static Method hiddenFieldGetter;
static {
try {
hiddenFieldGetter = Form.class.getDeclaredMethod("getHiddenFieldId");
hiddenFieldGetter.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#onBind()
*/
@Override
protected void onBind() {
super.onBind();
if (!(getComponent() instanceof IOnChangeListener))
{
throw new WicketRuntimeException("Behavior " + getClass().getName() +
" can only be added to an instance of a IOnChangeListener");
}
}
/**
* @param event
*/
public AjaxFormSubmittingChangeListenerBehavior(String event) {
super(event);
}
/**
* @see
org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onError(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
protected void onError(AjaxRequestTarget target) {
onSubmit(target);
}
/**
* @see
org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onEvent(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
protected void onEvent(AjaxRequestTarget target) {
HttpServletRequest httpServletRequest = ((WebRequest) getComponent()
.getRequest()).getHttpServletRequest();
Map parameters;
if (httpServletRequest instanceof MockHttpServletRequest) {
parameters = ((MockHttpServletRequest)
httpServletRequest).getParameterMap();
} else {
parameters = ((org.mortbay.jetty.Request)
httpServletRequest).getParameters();
}
parameters.put(getHiddenFieldId(getForm()),
getComponent().urlFor(IOnChangeListener.INTERFACE));
final FormComponent<?> formComponent = (FormComponent<?>) getComponent();
try {
if (isUpdateModel()) {
formComponent.inputChanged();
formComponent.validate();
if (!formComponent.hasErrorMessage()) {
formComponent.valid();
formComponent.updateModel();
}
}
super.onEvent(target);
} catch (RuntimeException e) {
Utils.errorLog(AjaxFormSubmittingChangeListenerBehavior.class, e);
onError(target);
}
}
/**
* @return boolean
*/
protected boolean isUpdateModel() {
return true;
}
/**
* @param form
* @return String
*/
private String getHiddenFieldId(Form<?> form) {
try {
Form<?> root = form.getRootForm();
return (String) hiddenFieldGetter.invoke(root);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2010/1/4 Steve Hiller <[email protected]>:
> Hi All,
>
> I'm having an issue when using WicketTester with a form that has 3
> DropDownChoice component, which are linked together using the
> AjaxFormComponentUpdatingBehavior. In the following code snippet,
> selecting from the "brands" DropDownChoice causes the "categories"
> DropDownChoice to be filled. Then, selecting from the "categories"
> DropDownChoice causes the "titles" DropDownChoice to be filled.
>
> All of this works fine. However, the call to formTester.submit()
> does not trigger the form's onSubmit method. Any ideas what I am
> doing wrong? This is using Wicket version 1.3.7.
>
> Thanks,
> Steve
>
> ----------
> WicketTester tester = new WicketTester(webapp);
> tester.setupRequestAndResponse();
>
> FormTester formTester = tester.newFormTester("mainPanel:officeSelectorForm");
> formTester.select("brands", 0);
> tester.executeAjaxEvent("mainPanel:officeSelectorForm:brands", "onchange");
> tester.assertComponentOnAjaxResponse("mainPanel:officeSelectorForm:categories");
> formTester.select("categories", 0);
> tester.executeAjaxEvent("mainPanel:officeSelectorForm:categories",
> "onchange");
> tester.assertComponentOnAjaxResponse("mainPanel:officeSelectorForm:titles");
> formTester.select("titles", 0);
>
> formTester.submit();
> ----------
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]