FormTester doesn't initialise values for RadioGroups
----------------------------------------------------

                 Key: WICKET-1411
                 URL: https://issues.apache.org/jira/browse/WICKET-1411
             Project: Wicket
          Issue Type: Bug
    Affects Versions: 1.3.0-final
            Reporter: David Shepherdson


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.)

-- 
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