[ 
https://issues.apache.org/jira/browse/WICKET-3171?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Igor Vaynberg resolved WICKET-3171.
-----------------------------------

    Resolution: Invalid

> Form components that become visible during submit always set their model to 
> null
> --------------------------------------------------------------------------------
>
>                 Key: WICKET-3171
>                 URL: https://issues.apache.org/jira/browse/WICKET-3171
>             Project: Wicket
>          Issue Type: Bug
>          Components: wicket
>    Affects Versions: 1.4.9
>         Environment: Mac OS X 10.6.4 running Java 1.6.0_22
>            Reporter: Jesse Barnum
>             Fix For: 1.4.14, 1.5-M4
>
>
> It seems that non-visible components initially have a convertedValue of null. 
> If they become visible during the form submit processing, they will write 
> this null value to their model. This can lead to data loss or exceptions.
> For example, with this sample code, if the checkbox to make the the 'name' 
> and 'age' fields visible is checked, null values are written to the data 
> object when the form is submitted.
> I'm not that familiar with the Wicket form processing internals, but it seems 
> to me that the solution is to gather a list of all visible components before 
> processing any of them, and then iterate through that list, ignoring new 
> components (with invalid state) that become visible. 
> ====
> package com.prosc.test;
> import org.apache.wicket.markup.html.WebMarkupContainer;
> import org.apache.wicket.markup.html.WebPage;
> import org.apache.wicket.markup.html.form.CheckBox;
> import org.apache.wicket.markup.html.form.Form;
> import org.apache.wicket.markup.html.form.TextField;
> import org.apache.wicket.model.CompoundPropertyModel;
> public class FormVisibilityTest extends WebPage {
>       private SampleBean bean = new SampleBean();
>       
>       public FormVisibilityTest() {
>               Form<SampleBean> form = new Form("form", new 
> CompoundPropertyModel(bean) );
>               add(form);
>               form.add( new CheckBox("showName" ) );
>               WebMarkupContainer nameSection = new WebMarkupContainer( 
> "nameSection" ) {
>                       public boolean isVisible() {
>                               //return true; //If everything is visible, 
> values are pulled from default SampleBean values and everything works as 
> expected
>                               return bean.isShowName(); //If visibility 
> starts off false when the form is initially submitted but then becomes true 
> during the submit process,  null values are submitted to the model
>                       }
>               };
>               form.add( nameSection );
>               nameSection.add( new TextField( "name" ) );
>               nameSection.add( new TextField( "age" ) );
>       }
> }
> ===
> package com.prosc.test;
> import java.io.Serializable;
> public class SampleBean implements Serializable {
>       private boolean showName = false;
>       private String name = "John";
>       private int age = 34;
>       public boolean isShowName() {
>               return showName;
>       }
>       public void setShowName( boolean showName ) {
>               this.showName = showName;
>       }
>       public String getName() {
>               return name;
>       }
>       public void setName( String name ) {
>               this.name = name;
>       }
>       public int getAge() {
>               return age;
>       }
>       public void setAge( int age ) {
>               this.age = age;
>       }
> }
> ====
> <html>
> <body>
> <form wicket:id="form" action="#">
>       <p>
>               <input type="checkbox" wicket:id="showName">Is name section 
> visible?
>       </p>
>       <p>
>       <span wicket:id="nameSection">
>               Name: <input type="text" wicket:id="name" value="Bob"><br>
>               Age: <input type="text" wicket:id="age" value="25">
>       </span>
>       </p>
>       
>       <p><input type="submit"></p>
> </form>
> </body>
> </html>
> =======
> package com.prosc.test;
> import org.apache.wicket.util.tester.WicketTester;
> import junit.framework.TestCase;
> public class FormVisibilityTestcase extends TestCase {
>       public void testSubmit() {
>               WicketTester tester = new WicketTester();
>               tester.startPage( FormVisibilityTest.class );
>               tester.setParameterForNextRequest( "form:showName", 
> Boolean.TRUE );
>               tester.submitForm( "form" ); //This causes an exception because 
> it tries to submit a null value to a setter that expects an int primitive for 
> 'age'
>               //org.apache.wicket.util.convert.ConversionException: Can't 
> convert null value to a primitive class: int for setting it on 
> com.prosc.test.sampleb...@5289e2f1
>       }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to