Reputation: 437
I have a RadioChoice component with an attached model. The radio choice is associated with a boolean field, hence I have to radio buttons (yes/no). My problem is that the no-button gets pre-selected when the page renders, because the boolean field is set to false. Is there a way to prevent this from happening, as I don't want to have a pre-selected value.
Any ideas are greatly appreciated.
Thank you!
Upvotes: 4
Views: 4841
Reputation: 4451
If you dont use Booleans, but rather primitives then you can override isSelected in RadioChoice:
@Override
protected boolean isSelected(Boolean object, int index, String selected) {
return false;
}
Upvotes: 0
Reputation: 1
Because your RadioChoice
component is attached with a model; when you write
yourRadioChoice.getValue()
you always get your model value not updated value. For prevent this you can use a temporary variable.
yourRadioChoice.add(new AjaxFormChoiceComponentUpdatingBehavior () {
private static final long serialVersionUID = 1662824638126944259L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
// you can get current value here and set to your temporary variable
// then use anywhere
}
});
Upvotes: 0
Reputation: 32427
From an HTML point of view, it's not recommended to omit the pre-selected radio value (see http://www.w3.org/TR/html401/interact/forms.html#radio). It's also rather irritating for the user, since there's no way to unselect all values within a radio group if he/she accidentally clicks one. You should probably rethink your UI - is there room for a third 'Unknown' option?
Upvotes: 3