umbraco / Umbraco.Forms.Issues

Public issue tracker for Umbraco Forms
29 stars 0 forks source link

Checkbox Caption contains the wrong value #1231

Closed jrunestone closed 4 weeks ago

jrunestone commented 4 weeks ago

Hi!

For a checkbox, the Model.Caption contains the value of Model.Name, not the actual caption from the form settings.

To get the caption I entered in the settings I have to use Model.GetSettingValue<string>(nameof(Model.Caption)). I noticed this is the way the caption is retrieved in the default theme as well. But why? I can access the correct caption value like this for a CheckBoxList.

image

image

image

Specifics

Umbraco: 13.3.1 Forms: 13.1.2

AndyButland commented 4 weeks ago

I can see the confusion, but I think this is correct. I'm assuming you are in the partial for a field here and thus talking about FieldViewModel.

Every field has a Caption property, which is what you retrieve with Model.Caption.

The field Name is set to the field's ID - and this what is set as the name of the input element's attribute - and retrieved via Model.Name.

Then each field has one or more settings, specific to the field. The checkbox has one called Caption - and this used for the text that goes next to the checkbox. In hindsight a different name could have been chosen to avoid this confusion.

In the default theme - on 13+ at least - we use this to retrieve this setting:

    var caption = string.Empty;
    Model.AdditionalSettings.TryGetValue("Caption", out caption);

What you have will work as well, but it's a little confusing. It only works because the name of the setting happens to be the same as nameof(Model.Caption), but you aren't actually retrieving anything from that property.

jrunestone commented 4 weeks ago

@AndyButland OK thanks for the clarification.

Just to be sure, do you mean that GetSettingValue("Caption") does NOT retrieve the same value as AdditionalSettings.TryGetValue("Caption")? I use nameof here just to avoid magic strings but I see what you mean, it's not a related property.

AndyButland commented 4 weeks ago

No, they should work the same. I was just pointing out that whilst it's a good idea to use nameof as you say, the point of it is to "return the name of the property you are referring to". It's only coincidence that this particular setting has a key that matches the name of the Caption property on FieldViewModel.