what's the preferred way to implement the submit mentioned in elm guide?

164 views
Skip to first unread message

诺铁

unread,
May 24, 2016, 11:01:17 PM5/24/16
to elm-d...@googlegroups.com
hi, 

  • Add a "Submit" button. Only show errors after it has been pressed.
I tried and find it not as easy as I thought. I have to put color and message in the model, I put the result code here: https://gist.github.com/notyy/d2f9fb4087e26d42e450a956c51f054d

it works, but...
would anyone please help me review the code and give me some hints?

Magnus Rundberget

unread,
May 25, 2016, 1:14:55 AM5/25/16
to Elm Discuss
I was about to write up in detail how you could do it, but that would take away some learning from you so here are some ideas:

- Putting color in the model strikes me as a bad idea, that should be a view concern that should be possible to derive from the model
- Maybe you could try to define a type describing a validation result (maybe containing None, Error String and OK). Look at your Msg type for a little inspiration
- Have a validation function that takes the model as input and return a validation result (type)
- Have a viewValidation function that based on the current value of validationResult in your model displays either an error with color or ok (or nothing in the case of not having validated anything yet).

give it a try, and if you are stuck we can go from there !

cheers
-magnus

诺铁

unread,
May 25, 2016, 4:24:10 AM5/25/16
to elm-d...@googlegroups.com
thanks magnus,

I tried to follow your suggestion, and updated my code accordingly. https://gist.github.com/notyy/d2f9fb4087e26d42e450a956c51f054d
the separation of concern is better now, but the code seems a little repetitious...... 

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Magnus Rundberget

unread,
May 25, 2016, 7:59:20 PM5/25/16
to Elm Discuss
Looks a lot better to me, nice and testable too !

- You could consider having Error as a generic tag and let it take a String. That way you could populate with an error message in your validate function. That would make the viewValidation function slightly simpler.

Btw you probably don't want to print out the password when displaying the error msg :-) sort of defeats the purpose of using input type password to hide it.

诺铁

unread,
May 25, 2016, 10:32:21 PM5/25/16
to elm-d...@googlegroups.com

- You could consider having Error as a generic tag and let it take a  String. That way you could populate with an error message in your validate function. That would make the viewValidation function slightly simpler.

feel good enough for now, thanks :)

Reply all
Reply to author
Forward
0 new messages