Malformed form-params upon submitting a form with empty (text input) fields

68 views
Skip to first unread message

Vineet Naik

unread,
Mar 7, 2014, 1:43:01 PM3/7/14
to comp...@googlegroups.com
Hi, 

I am using Compojure (version 1.1.6) for a webapp having an html form
in which some text input fields are non-mandatory. But when it is
submitted with one or more of these fields left blank, it results in a
malformed `form-params` map in the request handler.

for eg. suppose `qty`, and `qty_unit` are non-required form fields.

For debugging, I am just printing form-params in the handler

(defn add-entry
  [{form-params :form-params}]
  (println form-params))

If both the fields are left empty, following is printed:

{timezone -330, date 2014-03-07, qty_unit , qty , title dfsdfdfsdfsdfsd}

If just one of the fields is left empty, form-params turns out to be a
map with odd number of forms (but still no error):

{timezone -330, date 2014-03-07, qty_unit , qty 1, title dfsdfdfsdfsdfsd}

The form is submitted via ajax using jquery's `serializeArray` [1] and the
submitted form data displayed in chrome console looks correct. Also,
there are no other ring middlewares applied to this particular route.

What am I missing?

Regards,
Vineet

James Reeves

unread,
Mar 7, 2014, 1:59:59 PM3/7/14
to Compojure
The problem is how you're printing it. Try using "prn" instead:

    user=> (println "foo")
    foo
    user=> (prn "foo")
    "foo"

With println, strings are not quoted. So if you were to print a map with a blank string:

    user=> (println {"foo" "", "bar" "baz"})
    {foo , bar baz}
    user=> (prn {"foo" "", "bar" "baz"})
    {"foo" "", "bar" "baz"}

More formally, "print" and "println" just call ".toString" on everything that isn't a string, so if you mix in data structures, the result is a mishmash of unquoted strings and data structure syntax.

The "pr", "prn" and "pr-str" functions print the Clojure form (if it exists), so strings are quoted. For debugging purposes, it's usually better to use these, otherwise you'll have trouble with nils and blank strings.

- James


--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compojure+...@googlegroups.com.
To post to this group, send email to comp...@googlegroups.com.
Visit this group at http://groups.google.com/group/compojure.
For more options, visit https://groups.google.com/d/optout.

Vineet Naik

unread,
Mar 7, 2014, 2:18:03 PM3/7/14
to comp...@googlegroups.com
Oh! That clears up everything. 

Thanks for the quick reply. 
Reply all
Reply to author
Forward
0 new messages