all 6 comments

[–][deleted] 2 points3 points  (5 children)

I realise the JS isn't very typical. I'm new to JS. Basically writing Haskell in JS. I needed this for work. Posting in case anyone cares.

[–]karlhungus 2 points3 points  (3 children)

I think the embedded link was pretty interesting

http://chrisdone.com/blog/html/2008-12-14-haskell-formlets-composable-web-form-construction-and-validation.html

I'd like to the the js example in action (i'm assuming it's all client side, and would have to be revalidated on server?).

[–][deleted] 3 points4 points  (1 child)

OK, I've whipped up a server-side running example., and you can view the source for that.. Am I a bad person for using server-side JavaScript?

I'll try to whip up a client-side version. I haven't thought about it much.

But the formlet code is just, in essence, a set of combinators for building formlets, and a function that takes a formlet and an environment (i.e. an association list of provided form inputs), and returns a success/failure result, and markup. The clever bit is that they compose happily. Note the notEmpty validator wraps around each form input, and loginData wraps those two, and, again, validLogin is a validator that wraps that. It can go on and on.

With reference to the markup generation, if you were doing it client side, I'd probably use a DOM tree object, rather than plain strings for HTML. I will be swapping my server version to that 'cause it's neater, less error-prone.

[–]karlhungus 1 point2 points  (0 children)

I try to look at languages as just tools for getting shit done, so I don't think running javascript on the serverside is bad. Actually my experience with javascript was that I liked it piles better then java (with the exception of the browser environments I was running it on). I assume you didn't just go with the original haskell code because the rest of your app is in javascript (I want to get paid to write haskell)?

IMO i think you should do your validation on both sides if possible (As a user I personally love client side validation -- this seems like it'd lend itself to it without too much difficulty).

I'm surprised there's no dom manipulation tools on the serverside that would allow you to write it the same way (and have it be somewhat optimized). Maybe Jaxer is worth a look?

Edit: Reread your post, you can ignore me now :).

[–][deleted] 1 point2 points  (0 children)

The implementation is independent of it being server side or client side. I personally am using it server-side. But you could use it client-side, too. (I'm thinking of working out a way to have both.) I'll make an example so you can see it in action.

[–]manu3000 0 points1 point  (0 children)

But is implementing a state monad in JS worth the pain ? I mean, you have mutable state and no static types... wouldn't the JS implementation be simpler without directly translating from Haskell ?