[racket] formlet-fill

From: Jordan Schatz (jordan at noionlabs.com)
Date: Sat Nov 19 09:32:43 EST 2011

Let me see if I can explain what I'm after better. The app that I am
porting is mostly CRUD (Create Read Update Delete), one twist though is
that we never explicitly use create, if a user wants to create a new
record they actually edit a copy of the most recent record, so a form
always has values (though not necessarily the same "default" values).

If I understand the docs, then this:
http://noionlabs.com/formlets/save-formlet.rkt
Is the right way to do it. The general pattern is that to have a formlets
with values I need to have a formlet-maker function, that creates the
formlet, and I need to save that formlet for the formlet-process
function.

The docs seemed to suggest that I needed to save that formlet (and not
use formlet-maker to make a new one) for use with formlet-process, but
this http://noionlabs.com/formlets/generate-new-formlet.rkt does work. I
assume its not common/best practice though since its easy to break:
http://noionlabs.com/formlets/breaks.rkt

What I'd like to be able to write is:
http://noionlabs.com/formlets/formlet-fill.rkt

I don't really want to make a new formlet, or save it for use in the
processing step, I just want to be able to modify the values in the
formlet before sending it to the client.

Shalom,
Jordan

On Fri, Nov 18, 2011 at 04:10:29PM -0700, Jay McCarthy wrote:
> You need to make a new formlet that knows about the default values.
> 
> We could imagine a macro that makes this nicer, but in general it will need
> to be something that creates a new one.
> 
> Is there some reason you don't want to do that?
> 
> I can imagine something like (formlet-display) that took the old values,
> but then it would be more monad-like than arrow-like, and wouldn't really
> be a formlet. If you can clarify your use case, it may be worth
> investigating.
> 
> Jay
> 
> On Fri, Nov 18, 2011 at 1:13 PM, Jordan Schatz <jordan at noionlabs.com> wrote:
> 
> >
> > I have a pattern of user interaction that is common in my web app:
> >
> > I have a db record, I present it to the user in a form, the user edits
> > the form, submits it, server side logic validates it, and then returns
> > the same form with the updated record.
> >
> > I want to port the app to Racket, and I thought formlets would be just
> > the thing, but I don't see how to fill the values into the formlet
> > without making a new formlet... seems I need a (formlet-fill
> > (some-values)) to go along with the formlet-display and formlet-process.
> >
> > Here is what I have: (Which doesn't work)
> >
> > ----------------------------------------------------------------------
> > #lang web-server/insta
> >
> > (require web-server/formlets)
> >
> > (define record (hasheq 'title "this is a title"
> >                       'body "post body"))
> >
> > (define (start request)
> >  (edit-record request))
> >
> > (define (edit-record request)
> >  (local [(define (record-formlet our-values)
> >            (formlet
> >             (#%# ,{(text-input #:value (string->bytes/utf-8 (hash-ref
> > our-values 'title))) . => . title}
> >                  ,{(text-input #:value (string->bytes/utf-8 (hash-ref
> > our-values 'body))) . => . body})
> >             (values title body)))
> >
> >          (define (handle-response request)
> >            (define-values (title body)
> >              (formlet-process our-formlet request))
> >            (set! record (hasheq 'title title
> >                                 'body body))
> >            (start (redirect/get)))
> >
> >          (define (response-generator embed/url)
> >            (response/xexpr
> >             `(html
> >               (form ([action ,(embed/url handle-response)])
> >                     ,@(formlet-display our-formlet)
> >                     (input ([type "submit"]))))))]
> >
> >          (send/suspend/dispatch response-generator)))
> >
> > ----------------------------------------------------------------------
> >
> > It seems like this would be a very common pattern... what is the "normal"
> > way to do it?
> >
> > Thanks,
> > Jordan
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://lists.racket-lang.org/listinfo/users
> >
> 
> 
> 
> -- 
> Jay McCarthy <jay at cs.byu.edu>
> Assistant Professor / Brigham Young University
> http://faculty.cs.byu.edu/~jay
> 
> "The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.