You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by cmagnollay <cm...@gmail.com> on 2012/05/01 15:54:13 UTC

AutoCompleteField returning null

I am having an issue with DefaultCssAutoCompleteTextField where calling
getModelObject returns null. The autocomplete behavior itself is functioning
as intended, as I see the options come up, and am able to select one.

Definition:

itemField = new DefaultCssAutoCompleteTextField<String>("itemField", new
Model<String>()){

I then use an AjaxLink to try and dynamically add a label to a
WebMarkupContainer. That resulted in a nullpointerexception (because I do
some string manipulation of itemField.getModelObject() ). So I changed the
code to this to debug:

addItemLink = new AjaxLink<String>("addItemLink", new Model<String>("Add
Item")){
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) 
			{
				String selectedItem = itemField.getValue();
				String[] fields = selectedItem.split(" | ");
				
				info("Value of input: " + selectedItem);
				
				target.add(feedback);
                        }
};

Where feedback is a feedback panel. The feedbackpanel always shows that the
value it is getting back from itemField is empty. I tried getModelObject,
getValue to no avail. Is there something I am missing about the
AutoCompleteTextField or is there an error somewhere else in my code that is
messing up.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764.html
Sent from the Users forum 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


Re: AutoCompleteField returning null

Posted by Sebastien <se...@gmail.com>.
You're welcome.

You can not rely on the model object in that case because the form is not
proceed (it is sent but not proceed, because of defaultFormProcessing set
to false). For the model object of form components to be updated, the form
should be proceed and validators (if any) should not have failed.

Sebastien.

On Tue, May 1, 2012 at 8:21 PM, cmagnollay <cm...@gmail.com> wrote:

> No, this looks very promising. Thank you very much for elaborating. I still
> do not understand what is different about AutoCompleteTextField that it
> will
> not give me back the textual value (getModelObject()) reliably, but I will
> definitely try this out. Thank you for staying with me until we reached a
> possible conclusion.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601358.html
> Sent from the Users forum 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
>
>

Re: AutoCompleteField returning null

Posted by cmagnollay <cm...@gmail.com>.
No, this looks very promising. Thank you very much for elaborating. I still
do not understand what is different about AutoCompleteTextField that it will
not give me back the textual value (getModelObject()) reliably, but I will
definitely try this out. Thank you for staying with me until we reached a
possible conclusion.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601358.html
Sent from the Users forum 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


Re: AutoCompleteField returning null

Posted by Sebastien <se...@gmail.com>.
Hi again,

The discussion you point to is related to a case where it is not desired to
send the form. I may be wrong but if you need get the field value, you need
to have send the form (it is possible to post a single component without
sending the whole form but that's another discussion).

As you said, an AjaxLink does not send the form, whereas an AjaxButton
does. Setting addItemLink.setDefaultFormProcessing(false) seems to be a
good option here because it will send the form but will not process it and
you will get your field value (the user choice), using getValue() (but not
getModelObject, which might return null in that case). Thus, the onSubmit
(the one of the form) will not be called (once again, because the form has
not been processed), only the button#onSubmit will.

Hope this helps,
Sebastien.


On Tue, May 1, 2012 at 6:51 PM, cmagnollay <cm...@gmail.com> wrote:

> Hmm,
>
> The reason I used AjaxLink was because of this post I saw regarding
> stopping
> button from submitting to the form. Could you elaborate on why I cannot use
> AjaxLink, like Igor seems to recommend? Should I do
> setDefaultFormProcessingBehavior(false) to have the button not submit the
> form and cause a redirect?
>
> Or perhaps, I am misunderstanding what he means. He is talking about
> "attaching" a link to the button, but I suppose I am just unsure what he is
> talking about.
>
> Here is the link to the post I am referring to:
>
>
> http://apache-wicket.1842946.n4.nabble.com/A-button-that-does-NOT-submit-form-td1849785.html
>
>
> Thank you for your help once again.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601153.html
> Sent from the Users forum 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
>
>

Re: AutoCompleteField returning null

Posted by cmagnollay <cm...@gmail.com>.
Hmm,

The reason I used AjaxLink was because of this post I saw regarding stopping
button from submitting to the form. Could you elaborate on why I cannot use
AjaxLink, like Igor seems to recommend? Should I do
setDefaultFormProcessingBehavior(false) to have the button not submit the
form and cause a redirect?

Or perhaps, I am misunderstanding what he means. He is talking about 
"attaching" a link to the button, but I suppose I am just unsure what he is
talking about.

Here is the link to the post I am referring to:

http://apache-wicket.1842946.n4.nabble.com/A-button-that-does-NOT-submit-form-td1849785.html


Thank you for your help once again.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601153.html
Sent from the Users forum 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


Re: AutoCompleteField returning null

Posted by Sebastien <se...@gmail.com>.
Oops, I did not saw you was using an AjaxLink... You have to use an
AjaxButton instead.

Sebastien.

On Tue, May 1, 2012 at 5:51 PM, cmagnollay <cm...@gmail.com> wrote:

> Well, that is not the case unfortunately. Here is my code as stripped down
> as
> I could get it, to still supply information about what I am doing. If you
> have any questions I would be happy to answer.
>
>
> public class MyPage extends WebPage
> {
>        private TextField quantityField;
>
>        private Button doneButton = new Button("doneButton");
>
>        /**List of version numbers*/
>        private static final List versions = Arrays.asList(new Integer[]{6,
> 5, 4});
>        /**Selected version of item to search for*/
>        private int version = 6;
>
>        private AutoCompleteTextField itemField;
>        /**ListView of items currently selected by user*/
>        private ListView<Map.Entry&lt;Item, Integer>> itemList;
>        /**Container for list view so AJAX calls can reference it*/
>        private WebMarkupContainer itemListContainer;
>
>        /**Link prompting AJAX response to display the item that was added*/
>        private AjaxLink addItemLink;
>
>        private FeedbackPanel feedback = new FeedbackPanel("feedback");
>
>        private Map<Item, Integer> itemMap = new HashMap<Item, Integer>();
>
>        public MyPage(final PageParameters params)
>        {
>                super(params);
>
>                feedback.setOutputMarkupId(true);
>
>                Form<?> customerForm = new Form("customerForm"){
>
>                        private static final long serialVersionUID = 1L;
>
>                         protected void onSubmit()
>                        {
>                                //do stuff
>                        }
>                };
>                customerForm.setOutputMarkupId(true);
>
>                final DropDownChoice versionChoice = new
> DropDownChoice("versionChoice",
> new PropertyModel(this, "version"), versions);
>
>                itemField = new
> DefaultCssAutoCompleteTextField("itemField", new Model()){
>
>                        private static final long serialVersionUID = 1L;
>
>                        @Override
>                         protected Iterator getChoices(String text)
>                        {
>                                List items = new LinkedList();
>
>                                //fill items and do some string
> manipulation and database access
>
>                                Collections.sort(items);
>
>                                return items.iterator();
>                        }
>
>                };
>                itemField.setOutputMarkupId(true);
>
>                quantityField = new TextField("quantityField", new Model());
>                quantityField.setOutputMarkupId(true);
>
>                addItemLink = new AjaxLink("addItemLink", new Model("Add
> Item")){
>
>                        private static final long serialVersionUID = 1L;
>
>                        @Override
>                        public void onClick(AjaxRequestTarget target)
>                        {
>                                String selectedItem = itemField.getValue();
>                                String[] fields = selectedItem.split(" | ");
>
>                                info("Value of input: " + selectedItem);
>
>                                target.add(feedback);
>                        }
>
>                };
>                 addItemLink.setOutputMarkupId(true);
>
>                customerForm.add(itemField);
>                customerForm.add(versionChoice);
>                customerForm.add(quantityField);
>                customerForm.add(addItemLink);
>                customerForm.add(doneButton);
>
>                IModel<List&lt;Map.Entry&lt;Item, Integer>>> lvModel = new
> LoadableDetachableModel<List&lt;Map.Entry&lt;Item, Integer>>>(){
>                         private static final long serialVersionUID = 1L;
>
>                        @Override
>                         protected List<Entry&lt;Item, Integer>> load()
>                        {
>                                return new
> ArrayList<Map.Entry&lt;Item,Integer>>(itemMap.entrySet());
>                        }
>
>                };
>
>                itemList = new ListView<Map.Entry&lt;Item,
> Integer>>("itemList", lvModel){
>                         private static final long serialVersionUID = 1L;
>
>                        @Override
>                         protected void
> populateItem(ListItem<Map.Entry&lt;Item, Integer>> item)
>                        {
>                                Item i = item.getModelObject().getKey();
>                                int quantity =
> item.getModelObject().getValue();
>
>                                item.add(new Label(quantity + "x " +
> getItemText(i, true)));
>                        }
>                };
>
>                itemListContainer = new
> WebMarkupContainer("itemListContainer");
>                itemListContainer.add(itemList);
>                itemListContainer.setOutputMarkupId(true);
>
>                add(new NavigationBar("nav"));
>                add(feedback);
>                add(customerForm);
>                add(itemListContainer);
>        }
> }
>
>
>
> Thanks for your help.
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601022.html
> Sent from the Users forum 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
>
>

Re: AutoCompleteField returning null

Posted by cmagnollay <cm...@gmail.com>.
Well, that is not the case unfortunately. Here is my code as stripped down as
I could get it, to still supply information about what I am doing. If you
have any questions I would be happy to answer.


public class MyPage extends WebPage 
{
	private TextField quantityField;
	
	private Button doneButton = new Button("doneButton");
	
	/**List of version numbers*/
	private static final List versions = Arrays.asList(new Integer[]{6, 5, 4});
	/**Selected version of item to search for*/
	private int version = 6;
	
	private AutoCompleteTextField itemField;
	/**ListView of items currently selected by user*/
	private ListView<Map.Entry&lt;Item, Integer>> itemList;
	/**Container for list view so AJAX calls can reference it*/
	private WebMarkupContainer itemListContainer;
	
	/**Link prompting AJAX response to display the item that was added*/
	private AjaxLink addItemLink;
	
	private FeedbackPanel feedback = new FeedbackPanel("feedback");	
	
	private Map<Item, Integer> itemMap = new HashMap<Item, Integer>();
	
	public MyPage(final PageParameters params)
	{
		super(params);
		
		feedback.setOutputMarkupId(true);
		
		Form<?> customerForm = new Form("customerForm"){
			
			private static final long serialVersionUID = 1L;

			protected void onSubmit()
			{
				//do stuff
			}
		};
		customerForm.setOutputMarkupId(true);
		
		final DropDownChoice versionChoice = new DropDownChoice("versionChoice",
new PropertyModel(this, "version"), versions);
		
		itemField = new DefaultCssAutoCompleteTextField("itemField", new Model()){
			
			private static final long serialVersionUID = 1L;

			@Override
			protected Iterator getChoices(String text) 
			{
				List items = new LinkedList();
				
				//fill items and do some string manipulation and database access
				
				Collections.sort(items);
				
				return items.iterator();
			}
			
		};
		itemField.setOutputMarkupId(true);
		
		quantityField = new TextField("quantityField", new Model());
		quantityField.setOutputMarkupId(true);
		
		addItemLink = new AjaxLink("addItemLink", new Model("Add Item")){
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) 
			{
				String selectedItem = itemField.getValue();
				String[] fields = selectedItem.split(" | ");
				
				info("Value of input: " + selectedItem);
				
				target.add(feedback);
			}
			
		};
		addItemLink.setOutputMarkupId(true);
		
		customerForm.add(itemField);
		customerForm.add(versionChoice);
		customerForm.add(quantityField);
		customerForm.add(addItemLink);
		customerForm.add(doneButton);
		
		IModel<List&lt;Map.Entry&lt;Item, Integer>>> lvModel = new
LoadableDetachableModel<List&lt;Map.Entry&lt;Item, Integer>>>(){
			private static final long serialVersionUID = 1L;

			@Override
			protected List<Entry&lt;Item, Integer>> load() 
			{
				return new ArrayList<Map.Entry&lt;Item,Integer>>(itemMap.entrySet());
			}
	
		};
		
		itemList = new ListView<Map.Entry&lt;Item, Integer>>("itemList", lvModel){
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Map.Entry&lt;Item, Integer>> item) 
			{
				Item i = item.getModelObject().getKey();
				int quantity = item.getModelObject().getValue();
				
				item.add(new Label(quantity + "x " + getItemText(i, true)));
			}
		};
		
		itemListContainer = new WebMarkupContainer("itemListContainer");
		itemListContainer.add(itemList);
		itemListContainer.setOutputMarkupId(true);
		
		add(new NavigationBar("nav"));
		add(feedback);
		add(customerForm);
		add(itemListContainer);
	}
}



Thanks for your help.


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764p4601022.html
Sent from the Users forum 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


Re: AutoCompleteField returning null

Posted by Sebastien <se...@gmail.com>.
Hi,

> The feedbackpanel always shows that the value it is getting back from
itemField is empty.

itemField.getValue or getModelObject will returns empty if it is not in
your form... (this is the only reason I see regarding to the code you
supplied)

Sebastien.

On Tue, May 1, 2012 at 3:54 PM, cmagnollay <cm...@gmail.com> wrote:

> I am having an issue with DefaultCssAutoCompleteTextField where calling
> getModelObject returns null. The autocomplete behavior itself is
> functioning
> as intended, as I see the options come up, and am able to select one.
>
> Definition:
>
> itemField = new DefaultCssAutoCompleteTextField<String>("itemField", new
> Model<String>()){
>
> I then use an AjaxLink to try and dynamically add a label to a
> WebMarkupContainer. That resulted in a nullpointerexception (because I do
> some string manipulation of itemField.getModelObject() ). So I changed the
> code to this to debug:
>
> addItemLink = new AjaxLink<String>("addItemLink", new Model<String>("Add
> Item")){
>
>                        private static final long serialVersionUID = 1L;
>
>                        @Override
>                        public void onClick(AjaxRequestTarget target)
>                        {
>                                String selectedItem = itemField.getValue();
>                                String[] fields = selectedItem.split(" | ");
>
>                                info("Value of input: " + selectedItem);
>
>                                target.add(feedback);
>                        }
> };
>
> Where feedback is a feedback panel. The feedbackpanel always shows that the
> value it is getting back from itemField is empty. I tried getModelObject,
> getValue to no avail. Is there something I am missing about the
> AutoCompleteTextField or is there an error somewhere else in my code that
> is
> messing up.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/AutoCompleteField-returning-null-tp4600764.html
> Sent from the Users forum 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
>
>