You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by mashleyttu <ma...@gmail.com> on 2012/09/25 19:01:21 UTC

Model Binding Issue

Hi All,

I'm a little stumped here, and would like anyone's input to see what I am
doing wrong. I've created a new component that extends FormComponentPanel.
It contains a TextField component. When my form is submitted back to the
server, my model is not updated. :-( 

However, when I don't use this component, and I use a standard TextField
component my model is updated when my form is submitted back to the server.
I'm passing the model for my component into the TextField in my component,
so I'm not sure why it doesn't link back up on post back.

Here is my component (any comments on what I am doing wrong would be greatly
appreciated):


public class TextFieldPlaceHolder extends FormComponentPanel<String>{
	
	private TextField<String> txtInput;
	
	public TextFieldPlaceHolder(String id, IModel<String> model, String
placeHolderText) throws Exception{
		this(id,model,placeHolderText,false);
	}
	
	public TextFieldPlaceHolder(String id, IModel<String> model, String
placeHolderText, boolean isRequired) throws Exception {
		super(id, model);
		try{
			this.setType(String.class);
			txtInput = new TextField<String>("txtInput",model){

				private static final long serialVersionUID = 1L;
				
				protected void onComponentTag(ComponentTag tag){
					super.onComponentTag(tag);
					HashMap<String,String> myAttrs =
TextFieldPlaceHolder.this.setOnComponentTagTextField();
					if(myAttrs != null){
						for(Map.Entry<String, String> entry : myAttrs.entrySet()){
							String key = entry.getKey();
							String value = entry.getValue();
							
							String attrValue = tag.getAttribute(key);
							if(attrValue == null || attrValue.equals("")){
								tag.put(key, value);
							}else{
								tag.put(key, attrValue+" "+value);
							}
						}
					}
					String okKeyUpFunctionText = "if(this.value !=
''){$j('#lbl_"+this.getMarkupId()+"').hide();}else{$j('#lbl_"+this.getMarkupId()+"').show();}";
					tag.put("onkeyup",okKeyUpFunctionText);
				}
			};
			txtInput.setOutputMarkupPlaceholderTag(true);
			txtInput.setLabel(new DetachableStringModel(placeHolderText));
			FormComponentLabel lblLabel = new
FormComponentLabel("lblLabel",txtInput){
				private static final long serialVersionUID = 1L;
				
				@SuppressWarnings("rawtypes")
				protected void onComponentTag(ComponentTag tag){
					super.onComponentTag(tag);
					TextField txtInput = (TextField)
TextFieldPlaceHolder.this.get("txtInput");
					tag.put("id","lbl_"+txtInput.getMarkupId());
				}
			};
			Label lblText = new Label("lblText",txtInput.getLabel());
			lblText.setRenderBodyOnly(true);
			lblLabel.add(lblText);
			add(new
RequiredFieldIndicator("reqFldInd",isRequired).setOutputMarkupPlaceholderTag(true));
			add(lblLabel);
			add(txtInput);
		}catch(Exception e){
			throw e;
		}
	}


Thanks! Matt



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Model-Binding-Issue-tp4652337.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: Model Binding Issue

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Since you don't have multiple form components you don't need
FormComponentPanel and you can use plain Panel instead.
If you had some complex model which is represented by several form
components then you will need to override FCP's #convertInput() and
#updateModel() methods.

I'm not sure why the current code doesn't work though.

On Tue, Sep 25, 2012 at 8:01 PM, mashleyttu <ma...@gmail.com> wrote:
> Hi All,
>
> I'm a little stumped here, and would like anyone's input to see what I am
> doing wrong. I've created a new component that extends FormComponentPanel.
> It contains a TextField component. When my form is submitted back to the
> server, my model is not updated. :-(
>
> However, when I don't use this component, and I use a standard TextField
> component my model is updated when my form is submitted back to the server.
> I'm passing the model for my component into the TextField in my component,
> so I'm not sure why it doesn't link back up on post back.
>
> Here is my component (any comments on what I am doing wrong would be greatly
> appreciated):
>
>
> public class TextFieldPlaceHolder extends FormComponentPanel<String>{
>
>         private TextField<String> txtInput;
>
>         public TextFieldPlaceHolder(String id, IModel<String> model, String
> placeHolderText) throws Exception{
>                 this(id,model,placeHolderText,false);
>         }
>
>         public TextFieldPlaceHolder(String id, IModel<String> model, String
> placeHolderText, boolean isRequired) throws Exception {
>                 super(id, model);
>                 try{
>                         this.setType(String.class);
>                         txtInput = new TextField<String>("txtInput",model){
>
>                                 private static final long serialVersionUID = 1L;
>
>                                 protected void onComponentTag(ComponentTag tag){
>                                         super.onComponentTag(tag);
>                                         HashMap<String,String> myAttrs =
> TextFieldPlaceHolder.this.setOnComponentTagTextField();
>                                         if(myAttrs != null){
>                                                 for(Map.Entry<String, String> entry : myAttrs.entrySet()){
>                                                         String key = entry.getKey();
>                                                         String value = entry.getValue();
>
>                                                         String attrValue = tag.getAttribute(key);
>                                                         if(attrValue == null || attrValue.equals("")){
>                                                                 tag.put(key, value);
>                                                         }else{
>                                                                 tag.put(key, attrValue+" "+value);
>                                                         }
>                                                 }
>                                         }
>                                         String okKeyUpFunctionText = "if(this.value !=
> ''){$j('#lbl_"+this.getMarkupId()+"').hide();}else{$j('#lbl_"+this.getMarkupId()+"').show();}";
>                                         tag.put("onkeyup",okKeyUpFunctionText);
>                                 }
>                         };
>                         txtInput.setOutputMarkupPlaceholderTag(true);
>                         txtInput.setLabel(new DetachableStringModel(placeHolderText));
>                         FormComponentLabel lblLabel = new
> FormComponentLabel("lblLabel",txtInput){
>                                 private static final long serialVersionUID = 1L;
>
>                                 @SuppressWarnings("rawtypes")
>                                 protected void onComponentTag(ComponentTag tag){
>                                         super.onComponentTag(tag);
>                                         TextField txtInput = (TextField)
> TextFieldPlaceHolder.this.get("txtInput");
>                                         tag.put("id","lbl_"+txtInput.getMarkupId());
>                                 }
>                         };
>                         Label lblText = new Label("lblText",txtInput.getLabel());
>                         lblText.setRenderBodyOnly(true);
>                         lblLabel.add(lblText);
>                         add(new
> RequiredFieldIndicator("reqFldInd",isRequired).setOutputMarkupPlaceholderTag(true));
>                         add(lblLabel);
>                         add(txtInput);
>                 }catch(Exception e){
>                         throw e;
>                 }
>         }
>
>
> Thanks! Matt
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Model-Binding-Issue-tp4652337.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
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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