Accessing request params from defform :fields

2 views
Skip to first unread message

Paul Drummond

unread,
Oct 21, 2010, 6:35:42 AM10/21/10
to Sandbar Library
Hi Brenton,

Loving sandbar so far - thanks for all the great work!

Is there a way to get access to the request params from within
the :fields section of defform, something like this:

(forms/defform edit-form "/edit"
:fields [(forms/hidden :id)
(forms/textfield :my-field {:value (request (params :my-field))})
...)

I am trying to provide a default value for my-field even when the form
is for a new entity.

Cheers,
Paul D.

Brenton

unread,
Oct 21, 2010, 8:15:10 AM10/21/10
to Sandbar Library
Paul,

The only way to provide default values to the form is by using
the :defaults key with defform. See this example:

http://github.com/brentonashworth/sandbar/blob/master/src/sandbar/example/forms/complex.clj

It just takes a plane map.

I can update this so that it also takes a function of the request
which returns a map of default values. Would that enable you to do
what you a trying to do? Could you give me an example of why you need
access to the request in order to set the default value? I'm just
curious.

I will be traveling to clojure-conj today, but I should be able to
push this change by tomorrow.

Brenton

Paul Drummond

unread,
Oct 21, 2010, 8:40:09 AM10/21/10
to Sandbar Library
On Oct 21, 1:15 pm, Brenton <bashw...@gmail.com> wrote:
> Could you give me an example of why you need
> access to the request in order to set the default value? I'm just
> curious.

I just want to set the default value in the form to a value that is
provided in the url as a parameter but I didn't notice :defaults - I
will give that a try thanks.

>
> I will be traveling to clojure-conj today...

So jealous! :-)

Thanks
Paul.

Paul Drummond

unread,
Oct 21, 2010, 8:45:44 AM10/21/10
to Sandbar Library
I posted to soon!

I have the same problem in :defaults of course - I don't have access
to the params in the url in order to set the default value.

Maybe I am doing it wrong?

I guess what I am asking for is access to params (and the entire
request actually) in all sections of defform for maximum flexibility.
However, I might be just getting confused here - please put me right
if this doesn't make sense!

Thanks,
Paul.

Brenton

unread,
Oct 21, 2010, 8:03:28 PM10/21/10
to Sandbar Library
Paul,

I will make this change for the :defaults and let you know when it has
been pushed.

Brenton

Brenton

unread,
Oct 22, 2010, 7:35:34 PM10/22/10
to Sandbar Library
Paul,

I just pushed a change to sandbar (to Clojars) that should solve this
problem for you.

Currently defaults look like this:

:defaults {:city "unknown"}

This will set the default value of the field "city" to "unknown".

You may now also do this:

:defaults (fn [request] {:city (-> request ...something in the
request...}})

Let me know if this is what you needed. I think it is a good change.

Brenton

Paul Drummond

unread,
Oct 25, 2010, 6:30:51 PM10/25/10
to sandbar...@googlegroups.com
Yes, this is exactly what I was looking for - thank you for the quick
turn around!

Paul D.

Reply all
Reply to author
Forward
0 new messages