igor.vaynberg wrote:
>
> if you use wicket validators and the component is invalid the onerror()
> will
> be called instead of onupdate()
>
> the value is available via getformcomponent().getmodelobject() inside
> onupdate()
>
> -igor
>
> On 4/27/07, wicket0123 <[EMAIL PROTECTED]> wrote:
>> I want to use ajax to check if a field value is valid w/o submitting the
>> entire form. I add AjaxFormComponentUpdatingBehavior to my ajax object
>> (link/button/etc). But, I don't know how to retrieve the edit field's
>> value
>> inside the onUpdate method. See the code I have:
>>
>> fc = new RequiredTextField("userName");
>> fc.setLabel(new Model("User Name"));
>> add(fc);
>>
>> fc = new Button("checkId");
>> fc.add(new AjaxFormComponentUpdatingBehavior("onClick") {
>> protected void onUpdate(AjaxRequestTarget target)
>> {
>>
>> System.out.println("This gets called");
>>
>> // TODO: how to get the userName field's
>> value?
>>
>>
>> }
>> });
>>
>> add(fc);
>
But when you want the event fired as a result of a button click,
formcomponentupdatingbehavior only submits the component it is attached to
(the button). I had the same problem, so I wrote this behavior that acts
like a formcomponentupdatingbehavior but works like you want it to:
you would still attach it to the button, but you would override the
getComponentsToUpdate() method to return a list of FormComponents that
should be submitted as a result of this event, specifically, your textfield.
Does this work for you?
Chuck
=====================================
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import wicket.ajax.AjaxEventBehavior;
import wicket.ajax.AjaxRequestTarget;
import wicket.markup.html.form.FormComponent;
public abstract class AjaxMultipleComponentUpdatingBehavior extends
AjaxEventBehavior {
/**
* Constructor for AjaxMultipleComponentUpdatingBehavior
*
* @param event
*/
public AjaxMultipleComponentUpdatingBehavior(final String event) {
super(event);
}
/**
* Returns a List of FormComponents that should be updated upon this
event
*
* @return List
*/
protected abstract List<FormComponent> getComponentsToUpdate();
/**
* If there are any Components to update, then perform an Ajax POST
* Otherwise, fallback to an Ajax GET
*
* @see wicket.ajax.AjaxEventBehavior#getEventHandler()
*/
protected final CharSequence getEventHandler() {
StringBuffer serialize = new StringBuffer(1024);
Iterator<FormComponent> i = getFormComponents().iterator();
FormComponent fc = null;
while (i.hasNext()) {
fc = i.next();
if (!fc.getOutputMarkupId()) {
throw new IllegalArgumentException("cannot
update component that does
not have setOutputMarkupId property set to true.\n\tComponent: "
+ fc.toString());
}
if (serialize.length() > 0) {
serialize.append(" + ");
}
serialize.append("wicketSerialize(document.getElementById('");
serialize.append(fc.getMarkupId());
serialize.append("'))");
}
if (serialize.length() > 0) {
StringBuffer call = new StringBuffer(1024);
call.append("wicketAjaxPost('");
call.append(getCallbackUrl());
call.append("', ");
call.append(serialize);
return getCallbackScript(call, null, null);
}
else {
return getCallbackScript();
}
}
/**
* This internal method guarantees that the Behavior can rely on a
* non-null, non-empty list for processing.
*
* @return List<FormComponent>
*/
private List<FormComponent> getFormComponents() {
List<FormComponent> list = getComponentsToUpdate();
/*
* Make a clean copy of the supplied list
*/
if (list != null) {
list = new LinkedList<FormComponent>(list);
}
else {
list = new LinkedList<FormComponent>();
}
/*
* Attempt to also update the Component this behavior is
attached to
* (Insert the item as the first component to be updated)
*/
if (getComponent() instanceof FormComponent) {
list.add(0, (FormComponent) getComponent());
}
/*
* Remove disabled Components from the list of Components to
update
*/
Iterator<FormComponent> i = list.iterator();
while (i.hasNext()) {
FormComponent fc = i.next();
if (!fc.isEnabled() || !fc.isEnableAllowed()) {
i.remove();
}
}
return list;
}
/**
* @see wicket.ajax.AjaxEventBehavior#onCheckEvent(java.lang.String)
*/
protected void onCheckEvent(String event) {
if ("href".equalsIgnoreCase(event)) {
throw new IllegalArgumentException("this behavior
cannot be attached to
an 'href' event");
}
}
/**
* Called to handle any error resulting from updating form component.
Errors
* thrown from [EMAIL PROTECTED] #onUpdate(AjaxRequestTarget)} will not
be caught
here.
*
* The RuntimeException will be null if it was just a validation or
conversion
* error of the FormComponent
*
* @param target
* @param e
*/
protected void onError(AjaxRequestTarget target, RuntimeException e) {
if (e != null)
throw e;
}
/**
*
* @see
wicket.ajax.AjaxEventBehavior#onEvent(wicket.ajax.AjaxRequestTarget)
*/
protected final void onEvent(final AjaxRequestTarget target) {
for (FormComponent formComponent : getFormComponents()) {
try {
// make sure that the field is STILL able to be
saved...
if (formComponent.isEnableAllowed()) {
formComponent.inputChanged();
formComponent.validate();
formComponent.valid();
formComponent.updateModel();
}
}
catch (RuntimeException e) {
onError(target, e);
}
}
onUpdate(target);
}
/**
* Listener invoked on the ajax request. This listener is invoked after
the
* component's model has been updated.
*
* @param target
*/
protected abstract void onUpdate(AjaxRequestTarget target);
}
--
View this message in context:
http://www.nabble.com/how-to-get-a-form%27s-field-when-i-click-on-an-ajax-object-tf3660473.html#a10253552
Sent from the Wicket - User mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user