-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Handling forms #86
Comments
So far I have taken the approach of having an event (e.g. I would be interested in some kind of abstraction to reduce form layout / validation / event-handling boilerplate. I'd be tempted to try and copy something from the Elm universe, rather than Redux, since it is probably easier to translate from Elm rather than Javascript (but Haskell is more expressive than Elm so the |
Indeed, there's a lot of boilerplate that can be reduced, but, as I said, I'm not an experienced Haskell dev. Defining an event for each field is a lot of work, especially in my use case, since my forms have in total over 30 fields. In order to solve this I defined a Now, the tricky part is updating each field for every form. I defined a data FormField src a v = FormField {
_label :: Text, -- Used in prompts for this field (ex: "User name")
_value :: a, -- The kind of value of this field. Most of the times is Text
_setter :: src -> a -> src -- How to update this form field given a src (ex. Forms) and a new value
} ... Now, when I create a For example, a field There's a lot of boilerplate but this was the best I could do. You can see how this works in this toy project |
Hi! First of all, I want to thank you for this awesome library. I never thought that writing UI in Haskell could be this easy.
I'm having a bit of trouble when it comes to the design and implementation of forms, and I'm unable to find a full example of this use case, so I'm not sure about how to implement it.
Currently, I have a view with a few
Entries
, each for a specific input - let's sayname
,surname
,email
, and a submitButton
.The first solution that comes to mind is defining a field in my
State
that holds the current value of eachEntry
in some kind ofMap
. I can get the currentText
for eachEntry
using theonM #changed
function andentryGetText
, then I can fire anEvent
that updates theState
with the new value. This approach has a few advantages like on the fly validation and works fine for a small project, but let's say that my application has 20/30 forms. Would this scale?What I would like to do (don't know if this is the right way™) is to use a function that collects the text inside all my
Entries
and builds some kind ofMap
(id -> value
like in HTML forms) and fires anEvent
, something likeUserRegistrationEvent Fields
, whereFields
is a map of ids to values. This second approach would reduce the amount of code inside theState
record but sacrifices the on the fly validation. Also, sinceFields
is a kind ofMap
I'm a little worried about safety: I need to make sure that the values that I look up inside the map are valid - for example, if I look upphone-number
in my example I could get a runtime error.Please, let me know what you think. I'll update the issue with some code snippets from my use case as soon as possible.
Also: I looked into the React-Redux ecosystem since gi-gtk-declarative uses a similar design pattern for UIs and I found redux-form. It would be interesting to have a similar library for this amazing project.
Disclaimer: I primarily use Angular for my frontend projects so I'm not very confident in porting that library to Haskell
The text was updated successfully, but these errors were encountered: