SwiftUIFormValidator icon indicating copy to clipboard operation
SwiftUIFormValidator copied to clipboard

Issue with @FormField Property Wrapper and @Observable in viewModel in swiftUI iOS 17

Open avasveliya opened this issue 1 year ago • 1 comments

We recently updated our view model from using @ObservableObject to @Observable. The original code, which used ObservableObject, was working as expected and the latest value in timeZoneInput was correctly captured.

Old Code:

final class DummyClassViewModel: ObservableObject { }

@FormField(validator: {
    DropDownValidator<RoleDummy>(message: "Selection is required")
})
var timeZoneInput: RoleDummy = RoleDummy.empty
lazy var timeZoneInputValidation = _timeZoneInputInput.validation(manager: formManager)

Now, we have changed the class to use the @Observable macro, which is provided by the appple , as shown below:

New Code:

@Observable
final class DummyClassViewModel { }

However, after this change, we are encountering the following issue:

Error: Property wrapper cannot be applied to a computed property.

Solution Attempt: We tried adding @ObservationIgnored to the @FormField property wrapper, but as the name suggests, it causes the timeZoneInput to not receive the latest value.

Here is the code causing the issue:

@FormField(validator: {
    NonEmptyValidator(message: "This field is required.")
})
var timeZoneInput: RoleDummy = RoleDummy.empty

Expected Behavior: The timeZoneInput should be able to work with the property wrapper @FormField and retain the latest value as expected, without encountering the Property wrapper cannot be applied to a computed property error.

Could you please take a look at this issue and provide a solution or suggest a workaround? I have attached a screenshot for your reference.

Image

avasveliya avatar May 02 '25 05:05 avasveliya