How to use clojure namespaced keys with html forms?

Hi,

What are the best practices when submitting html forms in order to leverage namespaced keys?

For example, given the following form,

<form action="" method="get" class="form-example">
    <input type="text" name="name" id="name" required>
    <input type="email" name="email" id="email" required>
    <input type="text" name="street id="street required>
    <input type="submit" value="Subscribe!">
</form>

What is the best practice to get a map of clojure keys instead of just strings?

{ 
  :person/name "Bla bla"
  :person/email "[email protected]"
  :address/street "Planet Earth"
}

When sending json I believe you can process the form names before and do something to them ?!
But that feels clunky and you kind of need extra logic to do.
Also you would need to know the mapping beforehand or store the key in another attribute and switch the name -> value.
This can be done wither on the server or on the client but, again, feels clunky.
Is there a way to declare the keys and have the client / server use that instead of the name?

When submitting x-www-form-url-encoded I guess there is not much you can do.

I’ve checked the docs and there is a limited set of characters that are acceptable in the name:
Names can’t start with “:” also:

https://www.w3.org/TR/html401/interact/forms.html#control-name

1 Like

If I want to represent namespaced keywords in a context that does not understand them, and that does not allow the / character in identifiers, I tend to use __ (double underscore) as a stand-in. But that’s just my own default convention, there’s no universal approach or perfect solution for this.

Yes, you’ll have to handle that conversion yourself. Such is life when dealing with the outside world. But with a bit of logic set up at the boundaries this is fairly painless.

2 Likes

Yeah, you could for instance put this decoding in a Ring middleware handler, and use the clojure.core.keyword function (2-arity variant) to create the keys you want.

1 Like