You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by armandoxxx <ar...@dropchop.com> on 2010/08/09 11:27:22 UTC

Custom form component returning Invalid Type error ...

Hello ... 

I have a little problem with my custom form component. 

My component has a simple AbstractAutocompleteTextField (taken from wiki)
and a list. 
Main point is that you can search some beans (in my example Authors) with
autocomplete field and after bean is selected from autocomplete dropdown
list, the bean is added to the component selection list. So far so good.
Just to clarify my data object model and then to the problem. Data objects
are Beans and Author extends Bean. Bean has getter and setter methods for
ID. All other data objects (like Author) extend Bean and implement different
getters and setters. 
My component markup looks like this: 

		<wicket:panel>
			<div class="autoCompletePanel">
				Lorem ipsum 
				<input type="text" wicket:id="search-string" /> <!-- auto complete field
--->
				<div wicket:id="selection-list-wrapper"> <!-- selection list wrapper for
ajax update purposes -->
					<div wicket:id="selection-list"> <!-- selected beans items in a list
-->
						<input wicket:id="id" type="hidden" /> <!-- holds BEAN ID for
processing on submit -->
						[  X ]&nbsp;  <!-- bean name -->
					</div>
				</div>
				Lorem ipsum
			</div>
		</wicket:panel>



List populateItem method: 

		public void populateItem(final ListItem<T> theItem) {
			theItem.add(new AjaxLink<T>("remove-from-list", theItem.getModel()) {
				
				private static final long	serialVersionUID	= 1L;
				
				public void onClick(AjaxRequestTarget target) {
					T value	= getModelObject();
					if  (selections.contains(value)) {
						selections.remove(value);
					}
					target.addComponent(listWrapper);
				}
			});
			Bean bean	=  (Bean)theItem.getModelObject();
			theItem.add(new Label("text", new
Model<String>(((Displayable)bean).getDisplayTitle(language))));
			theItem.add(new HiddenField<String>("id", new
Model<String>(bean.getId())));
		}



I've written my custom BeanConverter and BeanValidator and added them to
component. 
Bean converter:

public class BeanConverter implements IConverter {
	
	private static final long	serialVersionUID	= -738427597240230763L;
	private static final Logger	LOG			=
LoggerFactory.getLogger(BeanConverter.class);
	
	
	
	@Override
	public Object convertToObject(final String theValue, final Locale
theLocale) {
		LOG.info("Got value into converter ! [{}]", theValue);
		Bean b	= new Bean();
		b.setId(theValue);
		return b;
	}
	
	
	@Override
	public String convertToString(final Object theObject, final Locale
theLocale) {
		LOG.info("Got object into converter ! [{}]", theObject);
		if (theObject instanceof Bean) {
			return ((Bean)theObject).getId();
		}
		return null;
	}

}


Bean Validator:

public class BeanValidator<T extends Bean> implements IValidator<T> {
	
	private static final Logger LOG			=
LoggerFactory.getLogger(BeanValidator.class);
	private static final long	serialVersionUID	= -6203025233458993869L;
	
	
	
	
	@Override
	public void validate(IValidatable<T> validatable) {
		T obj = validatable.getValue();
		LOG.info("Validating [{}]", obj);
		if (obj instanceof Bean) {
			try {
				UUID.fromString(((Bean)obj).getId());
			} catch (IllegalArgumentException iaEx) {
				LOG.error("Invalid bean id value ! [{}]", ((Bean)obj).getId());
				validatable.error(new
ValidationError().addMessageKey("bean.validator.error"));
			}
		}
		LOG.info("Validating [{}] passed!", obj);
	}

}


And now the explanation of the problem: 
When my form is submitted neither Converter or Validator are not called. Not
even method getInput() .. 
But somewhere in the process of submit, form invalidates with error saying
that items I selected are not valid "Author" objects and prints list of ID
(from list hidden fields).
exact Error:

'[id:[00030b2f-da22-47bc-acc7-01b8cee59cef] ,
id:[dbd7d63f-e15f-4cb5-a765-dd829bf603b5] ,
id:[84d34956-f25b-406e-8c59-d151fe891617] ]' is not a valid Author.


I don't get it ... any help would be greatly appreciated ... 

Kind regards

Armando
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Custom-form-component-returning-Invalid-Type-error-tp2318325p2318325.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


[SOLVED] Custom form component returning Invalid Type error ...

Posted by armandoxxx <ar...@dropchop.com>.
I finally figured it out ! 

If someone needs to submit a list of items in a form this is how I did it.

First I created a ListView (check list markup in my previous posts).
I keep ListView list items in private List object, so accessing them is easy
when form is submitted.
I implemented getInput() method that iterated my list items and builds a
string of all the items in a list. I decided to just put the ID values of
selected items to a string, separated by comma. 
After that I implemented a converter that converts this string back into
list of items.
After that i implemented a validator to check if my list items are valid. 

Kind regards

Armando








-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Custom-form-component-returning-Invalid-Type-error-tp2318325p2318694.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


UPDATE: Custom form component returning Invalid Type error ...

Posted by armandoxxx <ar...@dropchop.com>.
Hi ..

After playing some more with my component and submit ... I finally got call
to getInput() method.

Now the problem is that my ModelObject is ArrayList and if I call
.toString() method I get the string from the error out ! 

So can someone explain me what to return in getInput() method so list of my
beans would be processed as it should be ? 

kind regards

Armando 


-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Custom-form-component-returning-Invalid-Type-error-tp2318325p2318403.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org