You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Martin Makundi <ma...@koodaripalvelut.com> on 2008/02/19 06:23:57 UTC

Tabular form validation problem

Hi!

I have a simple html table with checkboxes and input text fields. My
goal is to validate that if the checkbox on the row is selected, the
respective input field should contain text. And vice versa, if the
input field contains text, the checkbox should be checked.

I have now constructed a nearly working version of the page. The
problems are as follows:
a) Checking the checkbox and submitting it, returns
checkBox.getModelObject() as False. Shouldn't it pick the value from
the form, which is checked?
b) If I write text in a textfield, the form gets invalidated due to
the "unchecked" checkbox on the row, and the textfield gets cleared. I
would like the typed text not to be cleared out.
c) If I type anything into the form after the feedback, and submit
again, the form is not validated, but the values remain on the screen,
and the feedback messages are cleared. This seems weird, as if the
form would be in some kind of tilt mode now. I get a warning on the
logs: IFormValidator in form `todoForm` depends on a component that
has been removed from the page or is no longer visible. Offending
component id `todo`.

Naturally I would like the form to behave more intuitively. Most
likely I am doing something significantly wrong here now. And the
warning message, why is that and how should I operate it insteaed?
Please have a look, here is my code:

<html xmlns:wicket="http://wicket.sourceforge.net">
<head>
<title>Welcome to TODO application</title>
</head>
<body>

<h2>Todo weekdays descriptions</h2>
<span wicket:id="feedback">Feedback messages will be here.</span>
<form name="todoForm" wicket:id="todoForm">
<table cellspacing="2" cellpadding="0" border="0">
	<thead>
		<tr>
			<th align="center">Date</th>
			<th align="center">Selected</th>
			<th align="left">Description</th>
		</tr>
	</thead>
	<tbody>
		<tr wicket:id="listView">
			<td align="right" NOWRAP><span wicket:id="date"></span></td>
			<td><input type="checkbox" wicket:id="todo"/></td>
      <td><input type="text" wicket:id="description" size="70"/></td>
		</tr>
	</tbody>	
</table>
<input type="submit" value="Save">
</form>
</body>
</html>

public class Todos extends WebPage {
  private static final String TODO_FORM = "todoForm";
  private static final String DESCRIPTION = "description";
  private static final String TODO = "todo";
  private static final String DATE = "date";
  private static final String LIST_VIEW = "listView";
  private List<Todo> todoList;

  public Todos() {
    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    User dummyUser = new User();

    Form todoForm = new Form(TODO_FORM);
    final List<FormComponent> formComponentsToBeValidated = new
LinkedList<FormComponent>();
    @SuppressWarnings("serial")
    ListView listView = new PropertyListView(LIST_VIEW,
getTodoList(dummyUser)) {
        protected void populateItem(ListItem item) {
          Todo todo = (Todo) item.getModelObject();
          item.add(new Label(DATE, new
Model(Convert.toString(todo.getTodoDay()))));
          CheckBox checkBox = new CheckBox(TODO, new
PropertyModel(todo, Todo.SELECTED));
          TextField textField = new TextField(DESCRIPTION, new
PropertyModel(todo, Todo.DESCRIPTION));
          item.add(checkBox);
          formComponentsToBeValidated.add(checkBox);
          item.add(textField);
          formComponentsToBeValidated.add(textField);
        }
    };

    @SuppressWarnings("serial")
    AbstractFormValidator formValidator = new AbstractFormValidator() {
      public FormComponent[] getDependentFormComponents() {
        return formComponentsToBeValidated.toArray(new
FormComponent[formComponentsToBeValidated.size()]);
      }

      public void validate(Form form) {
        Boolean textRequired = null;
        boolean checkedButDescriptionMissing = false;
        boolean descriptionGivenButUnchecked = false;
        for (FormComponent formComponent : formComponentsToBeValidated) {
          if (textRequired == null) {
            CheckBox checkBox = (CheckBox) formComponent;
            textRequired = (Boolean) checkBox.getModelObject();
            System.out.print("Checked: \"" + textRequired + "\" ");
          } else {
            TextField textField = (TextField) formComponent;
            boolean emptyField = Utils.isEmpty(textField.getValue());
            System.out.println("Field: \"" + textField.getValue() + "\"");
            if ((textRequired.booleanValue()) && (emptyField)) {
              if (!checkedButDescriptionMissing) {
                error(textField, "checked_but_description_missing");
                checkedButDescriptionMissing = true; // Show this only once
              }
            } else if ((!textRequired.booleanValue()) && (!emptyField)) {
              if (!descriptionGivenButUnchecked) {
                error(textField, "description_given_but_unchecked");
                descriptionGivenButUnchecked = true; // Show this only once
              }
            }
            textRequired = null;
          }
        }
      }
    };
    todoForm.add(formValidator);
    todoForm.add(listView);
    add(feedbackPanel);
    add(todoForm);
	}

  private List<Todo> getTodoList(User user) {
    if (todoList == null) {
      Date currentWeekStartDate = TakpSession.getWeekStartDate();
      todoList = TodoServices.browseWeek(currentWeekStartDate, user);
    }

    return todoList;
  }
}



**
Martin

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


Re: Tabular form validation problem

Posted by Igor Vaynberg <ig...@gmail.com>.
first step is to call listview.setreuseitems(true), that will get rid
of the disappearing input on invalidation. please read listview
javadoc.

-igor


On Feb 18, 2008 9:23 PM, Martin Makundi
<ma...@koodaripalvelut.com> wrote:
> Hi!
>
> I have a simple html table with checkboxes and input text fields. My
> goal is to validate that if the checkbox on the row is selected, the
> respective input field should contain text. And vice versa, if the
> input field contains text, the checkbox should be checked.
>
> I have now constructed a nearly working version of the page. The
> problems are as follows:
> a) Checking the checkbox and submitting it, returns
> checkBox.getModelObject() as False. Shouldn't it pick the value from
> the form, which is checked?
> b) If I write text in a textfield, the form gets invalidated due to
> the "unchecked" checkbox on the row, and the textfield gets cleared. I
> would like the typed text not to be cleared out.
> c) If I type anything into the form after the feedback, and submit
> again, the form is not validated, but the values remain on the screen,
> and the feedback messages are cleared. This seems weird, as if the
> form would be in some kind of tilt mode now. I get a warning on the
> logs: IFormValidator in form `todoForm` depends on a component that
> has been removed from the page or is no longer visible. Offending
> component id `todo`.
>
> Naturally I would like the form to behave more intuitively. Most
> likely I am doing something significantly wrong here now. And the
> warning message, why is that and how should I operate it insteaed?
> Please have a look, here is my code:
>
> <html xmlns:wicket="http://wicket.sourceforge.net">
> <head>
> <title>Welcome to TODO application</title>
> </head>
> <body>
>
> <h2>Todo weekdays descriptions</h2>
> <span wicket:id="feedback">Feedback messages will be here.</span>
> <form name="todoForm" wicket:id="todoForm">
> <table cellspacing="2" cellpadding="0" border="0">
>         <thead>
>                 <tr>
>                         <th align="center">Date</th>
>                         <th align="center">Selected</th>
>                         <th align="left">Description</th>
>                 </tr>
>         </thead>
>         <tbody>
>                 <tr wicket:id="listView">
>                         <td align="right" NOWRAP><span wicket:id="date"></span></td>
>                         <td><input type="checkbox" wicket:id="todo"/></td>
>       <td><input type="text" wicket:id="description" size="70"/></td>
>                 </tr>
>         </tbody>
> </table>
> <input type="submit" value="Save">
> </form>
> </body>
> </html>
>
> public class Todos extends WebPage {
>   private static final String TODO_FORM = "todoForm";
>   private static final String DESCRIPTION = "description";
>   private static final String TODO = "todo";
>   private static final String DATE = "date";
>   private static final String LIST_VIEW = "listView";
>   private List<Todo> todoList;
>
>   public Todos() {
>     final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
>     User dummyUser = new User();
>
>     Form todoForm = new Form(TODO_FORM);
>     final List<FormComponent> formComponentsToBeValidated = new
> LinkedList<FormComponent>();
>     @SuppressWarnings("serial")
>     ListView listView = new PropertyListView(LIST_VIEW,
> getTodoList(dummyUser)) {
>         protected void populateItem(ListItem item) {
>           Todo todo = (Todo) item.getModelObject();
>           item.add(new Label(DATE, new
> Model(Convert.toString(todo.getTodoDay()))));
>           CheckBox checkBox = new CheckBox(TODO, new
> PropertyModel(todo, Todo.SELECTED));
>           TextField textField = new TextField(DESCRIPTION, new
> PropertyModel(todo, Todo.DESCRIPTION));
>           item.add(checkBox);
>           formComponentsToBeValidated.add(checkBox);
>           item.add(textField);
>           formComponentsToBeValidated.add(textField);
>         }
>     };
>
>     @SuppressWarnings("serial")
>     AbstractFormValidator formValidator = new AbstractFormValidator() {
>       public FormComponent[] getDependentFormComponents() {
>         return formComponentsToBeValidated.toArray(new
> FormComponent[formComponentsToBeValidated.size()]);
>       }
>
>       public void validate(Form form) {
>         Boolean textRequired = null;
>         boolean checkedButDescriptionMissing = false;
>         boolean descriptionGivenButUnchecked = false;
>         for (FormComponent formComponent : formComponentsToBeValidated) {
>           if (textRequired == null) {
>             CheckBox checkBox = (CheckBox) formComponent;
>             textRequired = (Boolean) checkBox.getModelObject();
>             System.out.print("Checked: \"" + textRequired + "\" ");
>           } else {
>             TextField textField = (TextField) formComponent;
>             boolean emptyField = Utils.isEmpty(textField.getValue());
>             System.out.println("Field: \"" + textField.getValue() + "\"");
>             if ((textRequired.booleanValue()) && (emptyField)) {
>               if (!checkedButDescriptionMissing) {
>                 error(textField, "checked_but_description_missing");
>                 checkedButDescriptionMissing = true; // Show this only once
>               }
>             } else if ((!textRequired.booleanValue()) && (!emptyField)) {
>               if (!descriptionGivenButUnchecked) {
>                 error(textField, "description_given_but_unchecked");
>                 descriptionGivenButUnchecked = true; // Show this only once
>               }
>             }
>             textRequired = null;
>           }
>         }
>       }
>     };
>     todoForm.add(formValidator);
>     todoForm.add(listView);
>     add(feedbackPanel);
>     add(todoForm);
>         }
>
>   private List<Todo> getTodoList(User user) {
>     if (todoList == null) {
>       Date currentWeekStartDate = TakpSession.getWeekStartDate();
>       todoList = TodoServices.browseWeek(currentWeekStartDate, user);
>     }
>
>     return todoList;
>   }
> }
>
>
>
> **
> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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