Thanks,
I got it to work but I dont't understand why does RadioGroup has to have its
own model. RadioGroup has a constructor "RadioGroup(java.lang.String id)",
why does it exist if you can't create RadioGroup without its own model. It
seems somewhat misleading.
Here is the solution if someone else encounters this problem:
WebPage and Form:
public class TestRadio extends WebPage {
public TestRadio() {
add(new SomeForm("form"));
}
class SomeForm extends Form {
public SomeForm(String id) {
super(id, new CompoundPropertyModel(new
TestFormModel()));
TestRadioModel testRadioModel = new TestRadioModel();
RadioGroup radioGroup = new RadioGroup("radioGroup",
new PropertyModel(
testRadioModel, "radioGroup"));
add(radioGroup);
radioGroup.add(new Radio("radio1", new Model("some")));
radioGroup.add(new Radio("radio2", new Model("none")));
}
protected void onSubmit() {
super.onSubmit();
RadioGroup radioGroup = (RadioGroup)get("radioGroup");
String model = (String) radioGroup.getModelObject();
System.out.println(model);//prints out "some" or "none"
}
}
}
Html:
<form wicket:id="form">
<input type="radio" wicket:id="radio1" />
<input type="radio" wicket:id="radio2" />
<input type="submit"/>
</form>
RadioGroup Model:
public class TestRadioModel {
private String radioGroup;
private String radio1;
private String radio2;
// plus getters and setters
}
Michael O'Cleirigh wrote:
>
> Hi Goran,
>>
>> I have a form with a radio group. Everithing works (page renders without
>> errors) but I can't get selected radio in forms onSubmit method.
>> model.getSomeRadioGroup() returns null.
>>
>> It probably some logical error using the models. Does radioGroup have to
>> have its own model or something like that.
>>
>>
> RadioGroup does need to have its own model and it also needs to be
> included in the markup and it needs to wrap (be the parent) of the
> specific radio's.
>
> If your radio's are directly adjacent in the markup you might find the
> RadioChoice class easier since you can just set 1 model and pass in a
> list of the options.
>
>
>
>> Here is simplified example:
>>
>> FORM:
>> public SomeForm(String id) {
>> super(id, new CompoundPropertyModel(new SomeModel()));
>> RadioGroup radioGroup = new RadioGroup("someRadioGroup");
>> add(radioGroup);
>>
>> radioGroup.add(new Radio("radio1"));
>> radioGroup.add(new Radio("radio2"));
>> radioGroup.add(new Radio("radio3"));
>>
>> }
>>
> You need to set a model value for each radio since wicket will do a
> radioGroup.modelObject = selectedRadio.modelObject when the form is
> submitted.
>
>
>
>>
>> protected void onSubmit() {
>> super.onSubmit();
>> SomeModel model = (SomeModel) getModelObject();
>> System.out.println(model.getSomeRadioGroup()); // This returns null
>> System.out.println(model.getRadio1());// This returns null
>> System.out.println(model.getRadio2());// This returns null
>> System.out.println(model.getRadio3());// This returns null
>> }
>>
>> FORMS MODEL:
>> public class EmailSettingsModel {
>>
>> private String radio1;
>>
>> private String radio2;
>>
>> private String radio3;
>>
>> private String someRadioGroup;
>>
>> //... plus getters and setters
>> }
>>
>>
> I would change this so that the options for the radio were and
> enumeration:
>
> public enum EmailOptions { OPTION_1, OPTION_2, OPTION_3; }
>
> then just use a regular Model to hold the values:
>
> FORM:
> public SomeForm(String id) {
> super(id, new CompoundPropertyModel(new Model()));
> RadioGroup radioGroup = new RadioGroup("someRadioGroup", new Model
> (EmailOptions.OPTION_1);
> add(radioGroup);
>
> radioGroup.add(new EmailOptionRadio("radio1", new Model
> (EmailOptions.OPTION_1)));
> radioGroup.add(new EmailOptionRadio("radio2", new Model
> (EmailOptions.OPTION_2)));
> radioGroup.add(new EmailOptionRadio("radio3", new Model
> (EmailOptions.OPTION_3)));
>
> }
>
> Where emailOptionRadio overrides the Component.getConverter(Class c)
> method to return an IConverter for the EmailOptions enumeration like:
>
> /* (non-Javadoc)
> * @see org.apache.wicket.Component#getConverter(java.lang.Class)
> */
> @Override
> public IConverter getConverter(Class type) {
>
> return new IConverter () {
>
> /* (non-Javadoc)
> * @see
> org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
> java.util.Locale)
> */
> @Override
> public Object convertToObject(String value, Locale
> locale) {
>
> if (value.equals("1")) {
> return EmailOptions.OPTION_1;
> }
> else
> if (value.equals("2")) {
> return EmailOptions.OPTION_2;
> }
> if (value.equals("3")) {
> return EmailOptions.OPTION_3;
> }
> else return null;
>
>
>
> }
>
> /* (non-Javadoc)
> * @see
> org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object,
> java.util.Locale)
> */
> @Override
> public String convertToString(Object value, Locale
> locale) {
>
> EmailOptions opts = (EmailOptions)value;
>
> if (opts == EmailOptions.OPTION_1)
> return "1";
> if (opts == EmailOptions.OPTION_2)
> return "2";
>
> if (opts == EmailOptions.OPTION_3)
> return "3";
>
> else return "";
>
> }};
> }
>
>
>> HTML:
>>
>> <form wicket:id="form">
>>
>> <input wicket:id="radio1" type="radio"/>
>> <input wicket:id="radio2" type="radio"/>
>> <input wicket:id="radio3" type="radio"/>
>>
>> </form>
>>
>>
>
> Mike
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
View this message in context:
http://www.nabble.com/Radio-group%2C-model-tp16678100p16697337.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]