Hi!

One finishing touch is needed. The hidden field should be cleared
after ajax submit. It seems to work now.. feel free to critisize.

public abstract class FormSubmitAjaxChangeListenerCallDecorator
implements IAjaxCallDecorator {
  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.IAjaxCallDecorator#decorateOnFailureScript(java.lang.CharSequence)
   */
  public CharSequence decorateOnFailureScript(
      CharSequence arg0) {
    return getSetHiddenFieldValue("").append(arg0);
  }

  /**
   * @see 
org.apache.wicket.ajax.IAjaxCallDecorator#decorateOnSuccessScript(java.lang.CharSequence)
   */
  public CharSequence decorateOnSuccessScript(
      CharSequence arg0) {
    return getSetHiddenFieldValue("").append(arg0);
  }

  /**
   * @see 
org.apache.wicket.ajax.IAjaxCallDecorator#decorateScript(java.lang.CharSequence)
   */
  public CharSequence decorateScript(CharSequence arg0) {
    return getSetHiddenFieldValue(getChangeListenerUrl()).append(arg0);
  }

  /**
   * @param hiddenFieldId
   * @param value
   * @return
   */
  private AppendingStringBuffer getSetHiddenFieldValue(CharSequence value) {
    try {
      Form<?> root = getForm().getRootForm();
      String hiddenFieldId;
      hiddenFieldId = (String) hiddenFieldGetter.invoke(root);
      return new AppendingStringBuffer("document.getElementById('").append(
          hiddenFieldId).append("').value='").append(value).append(
          "';");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @return Form<?>
   */
  public abstract Form<?> getForm();

  /**
   * @return CharSequence
   */
  public abstract CharSequence getChangeListenerUrl();
}

**
Martin

2009/11/3 Martin Makundi <[email protected]>:
> Hi!
>
> I hope I found a solution usin IOnChangeListener.
>
> Please try this out and let me know if it works for you. Maybe it
> should be incorporated into Wicket?
>
> public abstract class FormSubmitAjaxChangeListenerCallDecorator
> implements IAjaxCallDecorator {
>  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.IAjaxCallDecorator#decorateOnFailureScript(java.lang.CharSequence)
>   */
>  public CharSequence decorateOnFailureScript(
>      CharSequence arg0) {
>    return arg0;
>  }
>
>  /**
>   * @see 
> org.apache.wicket.ajax.IAjaxCallDecorator#decorateOnSuccessScript(java.lang.CharSequence)
>   */
>  public CharSequence decorateOnSuccessScript(
>      CharSequence arg0) {
>    return arg0;
>  }
>
>  /**
>   * @see 
> org.apache.wicket.ajax.IAjaxCallDecorator#decorateScript(java.lang.CharSequence)
>   */
>  public CharSequence decorateScript(CharSequence arg0) {
>    Form<?> root = getForm().getRootForm();
>    String hiddenFieldId;
>    try {
>      hiddenFieldId = (String) hiddenFieldGetter.invoke(root);
>      return new AppendingStringBuffer("document.getElementById('").append(
>          
> hiddenFieldId).append("').value='").append(getChangeListenerUrl()).append(
>          "';").append(arg0);
>    } catch (Exception e) {
>      throw new RuntimeException(e);
>    }
>  }
>
>  /**
>   * @return Form<?>
>   */
>  public abstract Form<?> getForm();
>
>  /**
>   * @return CharSequence
>   */
>  public abstract CharSequence getChangeListenerUrl();
> }
>
>
> public abstract class AjaxFormSubmittingChangeListenerCheckBox extends
> CheckBox {
>  /**
>   * @param id
>   * @param model
>   */
>  public AjaxFormSubmittingChangeListenerCheckBox(String id,
> IModel<Boolean> model) {
>    super(id, model);
>    add(new AjaxFormSubmitBehavior(JavaScriptConstants.ONCHANGE) {
>     �...@override
>      protected void onError(AjaxRequestTarget target) {
>        AjaxFormSubmittingChangeListenerCheckBox.this.onError(target);
>      }
>
>     �...@override
>      protected void onSubmit(AjaxRequestTarget target) {
>        AjaxFormSubmittingChangeListenerCheckBox.this.onSubmit(target);
>      }
>
>      /**
>       * @see 
> org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getAjaxCallDecorator()
>       */
>     �...@override
>      protected IAjaxCallDecorator getAjaxCallDecorator() {
>        return new FormSubmitAjaxChangeListenerCallDecorator() {
>         �...@override
>          public CharSequence getChangeListenerUrl() {
>            return urlFor(IOnChangeListener.INTERFACE);
>          }
>
>         �...@override
>          public Form<?> getForm() {
>            return AjaxFormSubmittingChangeListenerCheckBox.this.getForm();
>          }
>
>        };
>      }
>    });
>  }
>
>  /**
>   * @param target
>   */
>  protected abstract void onSubmit(AjaxRequestTarget target);
>
>  /**
>   * @param target
>   */
>  protected void onError(AjaxRequestTarget target) {
>    onSubmit(target);
>  }
>
>  /**
>   * @param id
>   */
>  public AjaxFormSubmittingChangeListenerCheckBox(String id) {
>    this(id, null);
>  }
> }
>
>
> 2009/11/3 Martin Makundi <[email protected]>:
>> Hi!
>>
>> Maybe this is the solution:
>>
>>                        String url = 
>> getRequest().getParameter(getHiddenFieldId());
>>                        if (!Strings.isEmpty(url))
>>                        {
>>                                dispatchEvent(getPage(), url);
>>                        }
>>
>>
>> ??
>>
>> Attach a change listener with ajax form submit?
>>
>> **
>> Martin
>>
>> 2009/11/3 Martin Makundi <[email protected]>:
>>> Hi!
>>>
>>> I need to accomplish the following:
>>> 1. receive ajax onchange event from a formcomponent
>>> 2. receive "defaultformprocesing=false" style submit
>>> 3. repaint an area; I this is why I need the form to be really
>>> submitted (=rawinput but not validated).
>>>
>>> I have built a custom component FormSubmittingDropDownChoice.
>>>
>>> The problem is that even if I press another submit component (button,
>>> for example), the form allways assumes the formSubmitting component to
>>> be FormSubmittingDropDownChoice. Why? Because the findSubmittingButton
>>> is so simple that if FormSubmittingDropDownChoice has a value, it is
>>> the submitting component.
>>>
>>> Is there a workaround or fix? How could I achieve similar
>>> functionality in a working manner? This works well if I click the
>>> FormSubmittingDropDownChoice, otherwise it does not :(
>>>
>>> package com.tustor.wicket.common.reusables.formcomponents;
>>>
>>> public class FormSubmittingDropDownChoice<T> extends DropDownChoice<T>
>>> implements IFormSubmittingComponent {
>>>
>>>  public FormSubmittingDropDownChoice(String id, various constructor 
>>> options) {
>>>    super(id, choices, renderer);
>>>  }
>>>  /**
>>>   * @see 
>>> org.apache.wicket.markup.html.form.IFormSubmittingComponent#getDefaultFormProcessing()
>>>   */
>>>  public boolean getDefaultFormProcessing() {
>>>    return false;
>>>  }
>>>
>>>  /**
>>>   * @see 
>>> org.apache.wicket.markup.html.form.IFormSubmittingComponent#onSubmit()
>>>   */
>>>  public void onSubmit() {
>>>    // override
>>>  }
>>>
>>>  /**
>>>   * wicket-ajax:
>>>   *
>>>   * if (submitButton != null) {
>>>   *   s += Wicket.Form.encode(submitButton) + "=1";
>>>   * }
>>>   *
>>>   * @see org.apache.wicket.markup.html.form.FormComponent#getInputAsArray()
>>>   */
>>> �...@override
>>>  public String[] getInputAsArray() {
>>>    return MarkupUtils.filterSubmitIndicator(super.getInputAsArray());
>>>  }
>>>
>>>  /**
>>>   * @see 
>>> org.apache.wicket.markup.html.form.AbstractSingleSelectChoice#getModelValue()
>>>   */
>>> �...@override
>>>  public String getModelValue() {
>>>    String value = super.getModelValue();
>>>    if ("1".equals(value)) {
>>>      throw new IllegalStateException("1 not supported because of
>>> javaScript wicket-ajax:submitForm: function(form, submitButton)");
>>>    }
>>>    return value;
>>>  }
>>> }
>>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to