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 <martin.maku...@koodaripalvelut.com>:
> 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 <martin.maku...@koodaripalvelut.com>:
>> 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: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to