OnTopicCMS/OnTopic-Editor-AspNetCore

Bug: `DefaultValue` should be set on postback if value is `null`

Closed this issue · 2 comments

If the DefaultValue is set, attributes should have that Value set if a value isn’t already defined. This needs to happen after the first submit, to prevent a DefaultValue from overriding the InheritedValue if a BaseTopic is specified. Prior to that, during IsNew, the DefaultValue should be displayed as the InheritedValue to communicate what the value will be set to if left empty. IsRequired should be disabled if a DefaultValue is available, so it doesn’t need to be explicitly set.

The GetEditorViewModel<T>() method is configured to set the attribute value to the DefaultValue if IsNew and BaseTopic is null—except the logic is incorrect, and only sets DefaultValue if BaseTopic is not null.

For what it’s worth, BaseTopic will always be null if IsNew since the EditorController will save the topic as soon as the BaseTopic is set. As such, even if the logic was correct, it doesn’t really make sense here.

Regardless, the DefaultValue should ideally be set after submission, not before, so that it can be bypassed if the BaseTopic is set. That’s likely what this was attempting to accomplish, but is doing it at the wrong location. It should be done on postback instead. This will also need to be accounted for in the server-side validation of `IsRequired.

This was resolved by setting the DefaultValue on the AttributeType in the EditorController's Edit() action if the attribute value is null or empty, and the BaseTopic is not defined (b1dc5ec). In addition, the IsRequired validation is bypassed in this scenario, on both the client-side (ea1c325) and the server-side (00fa995). This was merged as part of 9c49c3c.