3 ms·
I've found it most useful to have form types that are derivable from db entities, but which are designed for parsing (and rendering) forms. What backend librar
by smallerfish 3y ago
I've found it most useful to have form types that are derivable from db entities, but which are designed for parsing (and rendering) forms.
What backend library do you use? Mine (javalin) gives me back forms as: `Map<String, List<String>>`, i.e. a map of form keys to list of values.
In your case, you have five keys that matter: capacity, memberType, members, from, to - so you could have a completely flat form object from which you derive your catA and catB entities easily enough.
I wrote a function which converts from the generic form structure to a more nested/typed structure, but I found that two levels of nesting becomes hard (maybe impossible) to reason about (consider lists with mismatched lengths, and how you'd handle them on nested objects); in any case, I'd be able to have:
data class MyForm(val catA: CatA, val catB: CatB)
...where CatB is flattened:
data class CatB(val from: OffsetDateTime, val to: OffsetDateTime)
...and then I'd be able to transform the form as:
request.toFormObject<MyForm>()
I've found this to hold up well so far. (I plan to write a few blog posts about the whole stack I'm playing with, including this -- subscribe on http://smaller.fish http://smaller.fish if interested in seeing them when published. No spam.)
- masfoobar 3y agoAppreciate your reply. I agree with your comment. I need to revist. Maybe I will be successful next time round. :-)