jkk / formative

Web forms for Clojure and ClojureScript - rendering, parsing, and validating
208 stars 18 forks source link

Custom inline anonymous functions for field type? #31

Closed lkrubner closed 11 years ago

lkrubner commented 11 years ago

Thank you for all the great work on formative.

I have a request: could you add a field "type" that allows us to add in an inline anonymous function for handling that field? For simple HTML formatting I'd rather define the function inline that invent a custom method with:

(defmethod render-field :hyperlink-value ;;;; code here)

My scenario:

Assume I have something like weblog software, so I have "posts" and "users". Each post is owned by 1 user. On the admin form that allows an admin to edit the "posts", I want the "user-id" field to be a hyperlink that could take an admin from the "post" record to the owning "user" record.

I could:

(defmethod render-field :hyperlink-value ;;;; code here)

Or I could make the presentational HTML fields work to this purpose (I assume).

But I'd rather just define an anonymous function inline, for relatively rare stuff like this.

jkk commented 11 years ago

If showing a link is a common pattern, your :hyperlink-value type with a :url key or something might work. What I usually do for such things is use a :labeled-html field type and set its :html key at runtime using merge-fields.

lkrubner commented 11 years ago

K, thanks. That is what I will do.

lkrubner commented 11 years ago

I guess I'll go with (defmethod render-field :hyperlink-value ;;;; code here). For my software, this is very common - I'm handing in a value (a string) and wrapping it in a simple HTML link. I do not want to set the value every time at runtime.