Issue with using the Forms API?

Topics: General, Troubleshooting, Writing modules
Apr 11, 2013 at 3:19 PM
I'm not sure if i've come across a bug, or if my usage of the Forms API is incorrect.
I am able to create forms, and have the data populated in the DB as I would expect, But I am seeing some funky behaviour regarding checkboxes. For example :
Func<IShapeFactory, dynamic> form = 
    shape => Shape.Form(
         Id: "DessertForm",
         _Icecream: Shape.Checkbox(
              Id: "icecream", Name: "icecream",
              Title: T("Do you want Ice Cream?")
          )
      );
will render a checkbox, and when the user saves the form, the database saves it like:
<Form><icecream>on</icecream></Form>
so when you go to edit the form again, the checkbox will never be selected as in the DefaultFormManager class of Orchard.Forms the BindValue method appears to be trying to compare the value "on" to true?
 shape.Checked = Convert.ToString(value.AttemptedValue) == Convert.ToString(shape.Value);
I assume the same issue exists for Radio buttons...
So my question is, Am I missing something in my setup of the Form in the code, or is this a bug in Forms API?

Any and all assistance would be most appreciated.
Developer
Apr 13, 2013 at 12:10 AM
In your last code snippet, an equality check is done between the attempted value and the actual value, so I don't think it's comparing "on" with true.
In any case, you can set the value of the checkbox yourself, e.g.:
Func<IShapeFactory, dynamic> form = 
    shape => Shape.Form(
         Id: "DessertForm",
         _Icecream: Shape.Checkbox(
              Id: "icecream", Name: "icecream",
              Title: T("Do you want Ice Cream?"),
              Value: "true"
          )
      );
Apr 15, 2013 at 2:59 PM
Yep, that was the part I was misisng.. Thanks for that.