Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-1411

FormTester doesn't initialise values for RadioGroups

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.0-final
    • Fix Version/s: 1.3.3
    • Component/s: None
    • Labels:
      None

      Description

      In the constructor for FormTester, a visitor runs through all the form components and sets their initial values in the request. However, the visitor only deals with components that are instances of specific classes: currently AbstractTextComponent, DropDownChoice, RadioChoice, CheckBox, ListMultipleChoice and CheckGroup.

      Notably absent from that list is RadioGroup; if a RadioGroup is present in the form, it won't have any value set in the request and therefore when the form is submitted, if it's a required field the form validation will fail.

      We're working around this by adding the following code to the onFormComponent(FormComponent) method of the listener:

      else if (formComponent instanceof RadioGroup)
      {
      final String value = formComponent.getValue();
      Object choiceValue = formComponent.visitChildren(Radio.class, new IVisitor()
      {

      public Object component(Component component)
      {
      // O-P Preserve old escaping value, then turn escaping off
      // so that values aren't escaped unnecessarily.
      boolean oldEscaping = component.getEscapeModelStrings();
      component.setEscapeModelStrings(false);
      Radio radio = (Radio)component;
      String radioValue = radio.getValue();
      // O-P Restore the previous escaping setting.
      component.setEscapeModelStrings(oldEscaping);
      if (radio.getModelObject().toString().equals(value))

      { return radioValue; }

      return CONTINUE_TRAVERSAL;
      }

      });
      if (choiceValue != null)

      { setFormComponentValue(formComponent, (String) choiceValue); }

      }

      (See issue WICKET-1094 for the reason for the 'O-P' comments about escaping, though I'm not sure that they're really needed in this case.)

      Would it also be sensible to add a fallback case; something like the following?

      else

      { setFormComponentValue(formComponent, formComponent.getValue()); }

      That would deal with any user-defined components that are subclasses of FormComponent but not of one of the existing Wicket components. (Though I'm not sure how common that is; we haven't done it ourselves.)

        Attachments

          Activity

            People

            • Assignee:
              ivaynberg Igor Vaynberg
              Reporter:
              david.shepherdson David Shepherdson
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: