Get output record from Formless component?

Is there a nice way to get Maybe MyRecord out of a Formless component? I need a Just if all fields validate, and Nothing otherwise.

Bigger picture: I am making a Formless component with a “Cancel” button, so the component Output type needs to be Maybe MyRecord instead of just MyRecord for representing the “cancel” outcome. I’ve got my custom Action type and handleAction function, but I am stuck on getting MyRecord out of the form.

The Formless.Retrieve module has helpers for this kind of thing.

You can use Formless.Retrieve.getOutput to retrieve a single field from the form, with the resulting type of Maybe o (as there may not be an output).

Similarly, you can use Formless.Retrieve.getOutputAll to get all of the output fields, if they exist; iirc this gives you a record where each key has a Maybe value. Still not what you want, but sometimes useful!

Unfortunately, there’s not a helper that takes you from a record of Maybe values to a Maybe record of values. However, you can use sequenceRecord to do this:

So combining getOutputAll with sequenceRecord should do what you want.

1 Like

getOutputAll with sequenceRecord works perfectly, thanks!

1 Like