Anonymous avatar Anonymous committed 39f6879

[svn] * Sync'd form_options, form_tag helpers. form_tag's form function can now
accept other HTTP methods, and will include a hidden field for them if
its not 'get' or 'post'.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.2 (**svn**)
+* Sync'd form_options, form_tag helpers. form_tag's form function can now
+  accept other HTTP methods, and will include a hidden field for them if
+  its not 'get' or 'post'.
 * Sync'd number helper, added number unit tests.
 * Added markdown.py (python-markdown) for new markdown support in text helper.
 * Added textile.py (PyTextile) for new textilize support in text helper.

webhelpers/rails/form_options.py

 """
 Form Options Helpers
 """
+# Last synced with Rails copy at Revision 4331 on Aug 19th, 2006.
+# Purposely left out a few redundant options_for_collection stuff.
+
 
 from webhelpers.util import html_escape
 

webhelpers/rails/form_tag.py

 """
 Form Tag Helpers
 """
+# Last synced with Rails copy at Revision 4374 on Aug 19th, 2006.
+
 from urls import confirm_javascript_function
 from tags import *
 from webhelpers.util import html_escape
 
-def form(url, **options):
+def form(url, method="post", multipart=False, **options):
     """
     Starts a form tag that points the action to an url. 
     
-    The url options should be given either as a string, or as a ``url()`` function. The
-    method for the form defaults to POST.
+    The url options should be given either as a string, or as a ``url()``
+    function. The method for the form defaults to POST.
     
     Options:
 
-    * ``multipart`` - If set to True, the enctype is set to "multipart/form-data".
-    * ``method`` - The method to use when submitting the form, usually either "get" or "post".
+    ``multipart``
+        If set to True, the enctype is set to "multipart/form-data".
+    ``method``
+        The method to use when submitting the form, usually either "get" or 
+        "post". If "put", "delete", or another verb is used, a hidden input
+        with name _method is added to simulate the verb over post.
     
     """
-    o = { "method": "post" }
-    o.update(options)
-    if 'multipart' in o:
-        o["enctype"] = "multipart/form-data"
-        del o['multipart']
+    if multipart:
+        options["enctype"] = "multipart/form-data"
+    
     if callable(url):
         url = url()
     else:
         url = html_escape(url)
-    o["action"] = url
-    return tag("form", True, **o)
+    
+    method_tag = ""
+    
+    if method in ['post', 'get']:
+        options['method'] = method
+    else:
+        options['method'] = "post"
+        method_tag = tag('input', type="hidden", name_="_method", value=method)
+    
+    options["action"] = url
+    return tag("form", True, **options) + method_tag
 
 start_form = form
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.