The WHATWG Blog

Please leave your sense of logic at the door, thanks!

Proposing URI Templates for WebForms 2.0

by Mike Schinkel in Forms

I recently had an off-list email conversation with Ian Hickson about the possibility of adding URI Template support to the FORM element in WebForms 2.0. I've documented our conversation and my response to his suggestion to use server-side direct or Javascript instead over at the Well Designed URL Initiative blog. However, I've provided some examples of what I'm proposing below:

<form 
  action="http://foo.com/model"
  template="http://foo.com/{make}/{model}/"
  method="get">
  <input type="text" name="make" />
  <input type="text" name="mode" />
  <input type="submit" />
</form>
<form
  action="http://www.myblog.com/topic"
  template="http://www.myblog.com/{topic}/"
  method="post">
  <select name="topic">
    <option value="first">My 1st Post</option>
    <option value="second">My 2nd Post</option>
    <option value="third">My 3d Post</option>
  </select>
  <input type="text" name="comment">
  <input type="submit">
</form>
<form 
  action="http://blog.whatwg.org/topic"
  template="http://blog.whatwg.org/{topic}"
  method="post">
  <select name="topic">
    <option value="feed-autodiscovery">
       Feed Autodiscovery
    </option>
    <option
value="text-content-checking">
        textContent Checking
    </option>
    <option value="checker-bug-fixes">
        Bug Fixes
    </option>
    <option
        value="significant-inline-checking">
        Significant Inline Checking
    </option>
    <option value="charmod-norm-checking">
        Charmod Norm Checking
    </option>
    <option
value="proposing-features">
        Proposing features
    </option>
  </select>
  <input type="submit">
</form>

One point I made on the WDUI blog that I also I want to make here is that is seems from my time on the REST-discuss list that many of the REST experts tend toward using (what I call well-designed URLs, i.e. URLs where the resource is identified by path instead of query string. With WebForm 2.0's pending support of PUT and DELETE, it would be just short of a crime not to include support for posting to clean URLs in WebForms 2.0.

So I really hope that Ian and the WHATWG can see their way clear to consider adding this feature to WebForms 2.0. And if the main issue with it is needing to have it written up for inclusion in the spec, I'm more than happy to help.

9 Responses to “Proposing URI Templates for WebForms 2.0”

  1. How do you escape real usage of ‘{‘ and ‘}’?

    I might not intend to use template in my uri, but I might have action=”{something}” literally, and I might even have an input element in my form with name=”something”, yet I am unaware of uri templates, so I need a system to be compatible with my legacy form.

  2. AFAICT, the proposal above suggests a new attribute for templates, it does not reuse the ‘action’ attribute. So if you have curly braces in your URL, nothing special will happen, because you’ll be using ‘action’ instead of ‘template’. This approach also allows backward compatibility: if URL templates aren’t supported, then the form will fall back to a regular query string appended to the ‘action’.

  3. Yep, my oversight. Good deal. We still need an escape mechanism for ‘{‘ and ‘}’, though, so you can use them in templates. My first thought was %xx, but keep in mind that HTML authors have never had to escape URL characters, except to use entities ‘<‘ and ‘&eq;’ in XHTML. (if i understand correctly). So it will be forging new ground to come up with an escaping language for templates. Needs a little thought.

  4. fantasai says:

    This approach also allows backward compatibility: if URL templates aren’t supported, then the form will fall back to a regular query string appended to the ‘action’.

    …or to a “regular POST” (or PUT) to the ‘action’.

    But what happens when method="delete"? I can see how it would work when templates are supported, but if they aren’t, you probably don’t want an HTTP DELETE to be sent to the ‘action’…

  5. fantasai >> AFAICT, the proposal above suggests a new attribute for templates, it does not reuse the ‘action’ attribute.

    Correct, as Mark Baker pointed out if I used a URI Template in the action field it would break backward compatibility, which is why I changed my original idea to include the template attribute.

    Thomas Broyer>> But what happens when method=”delete”? I can see how it would work when templates are supported, but if they aren’t, you probably don’t want an HTTP DELETE to be sent to the ‘action’…

    Well, since we don’t have to worry about backward compatibility on DELETE, there wouldn’t be any existing forms offering DELETE and an action attribute. Not knowing what’s actually best, I can suggest one of two potentials:

    1.) Prohibit client agents from submitting DELETE to the action URI.
    2.) Allow DELETE to the action URI but warn against using DELETE with action.

    If #2 is the best route, it would only be new code that could potentially be affected (unless someone was dynamically composing pages and added “PUT” and “DELETE” as options with testing their code!) All in all, I think it a problem that would be pretty reasonable to handle from a backwards compatibility perspective. Or am I missing something?

    BTW, I originally forgot to hyperlink “URI Template” to provide background for those not familiar with the excellent work Joe Gregorio and friends are doing on the URI Template specification on [[email protected]]. I’ve just now updated the post to include that hyperlink; check it out.

  6. Thomas: html forms only support the DELETE method in this new proposal, so older browsers do not support that anyway. When the DELETE method is supported, it’s likely that templates are supported as well. (both in WebForms 2.0). And yes, I would think that you do want the HTTP DELETE to be sent to the ‘action’ URI, you’d just have to make sure that this URI would be correct. (for example: the id of the document to be deleted would be in the URI)

    Next: What about enctype=”application/json”?

  7. Right, sorry for the miss: using template= solves backward compat problem.

    You still need to come up with a rule for escaping. I am sure % encoding will do fine, but you have to be aware that nowhere else in HTML do you ordinarily have to % encode anything… e.g. you can leave space characters in action=, href=, etc. Now you will be telling authors to % encode ‘{‘ and ‘}’… it’s a matter of introducing a new convention.

  8. When the DELETE method is supported, it’s likely that templates are supported as well. (both in WebForms 2.0). And yes, I would think that you do want the HTTP DELETE to be sent to the ‘action’ URI, you’d just have to make sure that this URI would be correct.