WPF-Forge/Forge.Forms

Extra data for checkboxes

hashitha opened this issue · 7 comments

Is there any way we can attach extra details for checkboxes so we can retrieve these in the JSON data.

something like this

 <input
    type="bool"
    name="is-PancreasAbnormal"
    uncheckedText="Pancreas is normal"
    checkedText="Pancreas is abnormal and shows echogenicity"
    label="Abnormal Echogenicity"></input>

then the final JSON object would look like the following if the checkbox is ticked

{
  "is-PancreasAbnormal": {
    "value": true,
    "text": "Pancreas is abnormal and shows echogenicity"
  },
  "LastName": "doe",
  "DateOfBirth": "2018-10-08T00:00:00"
}

You could attach metadata to the field:

<input type="bool" meta-text-unchecked="... is-normal" meta-text-checked="... is abnormal"

Then, when you are serializing the form to JSON, you can check each field for metadata.

var jobject = new JObject();
var myElements = CompiledDefinition.GetElements();
foreach (var keyValuePair in (IDictionary<string,object>)MyModel)
{
    var field = myElements.First(e => e is DataFormField df && df.Key == keyValuePair.Key);
    if (field.Metadata.TryGetValue("text-checked", out string checkedText))
    {
        // Do something when serializing
    }

    if (field.Metadata.TryGetValue("text-unchecked", out string uncheckedText))
    {
        // Do something when serializing
    }

    jobject[keyValuePair.Key] = keyValuePair.Value;
}

I can't get this to work.

This is my current code to convert to JSON

 public void HandleAction(IActionContext actionContext)
{
  ExpandoObject formDataObject = (actionContext.Model as ExpandoObject);
  dynamicFormData = Newtonsoft.Json.JsonConvert.SerializeObject(formDataObject);
}

if I change this to something like this then I get error field.Metadata is not defined. It doesn't look like FormElement has a Metadata property

public void HandleAction(IActionContext actionContext)
        {

            var jobject = new JObject();
            var myElements = CompiledDefinition.GetElements();

            foreach (var keyValuePair in (IDictionary<string, object>)actionContext.Model)
            {
                var field = myElements.First(e => e is DataFormField df && df.Key == keyValuePair.Key);
                
                if (field.Metadata.TryGetValue("text-checked", out string checkedText))
                {
                    // Do something when serializing
                }

                if (field.Metadata.TryGetValue("text-unchecked", out string uncheckedText))
                {
                    // Do something when serializing
                }

                jobject[keyValuePair.Key] = keyValuePair.Value;
            }
 
        }

Have you added the below attributes to the XML?

meta-text-unchecked="..."
meta-text-checked="..."

Yes, but I am getting a compile error

image

Doesn't look like FormElement has Metadata field
image

Maybe you have an outdated version, are you using the NuGet build?

Thanks, @edongashi I got it working now. I was using an outdated version. I can't use the NuGet build because of anything above version 2.2.0 of Humanizer.Core breaks our UI for some reason. So I had to change that version in Forge and build.

Thanks for the info, do you have any idea why humanizer is causing problems?