1 | """ |
---|
2 | Form Tag Helpers |
---|
3 | """ |
---|
4 | # Last synced with Rails copy at Revision 4374 on Aug 19th, 2006. |
---|
5 | |
---|
6 | from urls import confirm_javascript_function |
---|
7 | from tags import * |
---|
8 | from webhelpers.util import html_escape |
---|
9 | |
---|
10 | def form(url, method="post", multipart=False, **options): |
---|
11 | """ |
---|
12 | Starts a form tag that points the action to an url. |
---|
13 | |
---|
14 | The url options should be given either as a string, or as a ``url()`` |
---|
15 | function. The method for the form defaults to POST. |
---|
16 | |
---|
17 | Options: |
---|
18 | |
---|
19 | ``multipart`` |
---|
20 | If set to True, the enctype is set to "multipart/form-data". |
---|
21 | ``method`` |
---|
22 | The method to use when submitting the form, usually either "get" or |
---|
23 | "post". If "put", "delete", or another verb is used, a hidden input |
---|
24 | with name _method is added to simulate the verb over post. |
---|
25 | |
---|
26 | """ |
---|
27 | if multipart: |
---|
28 | options["enctype"] = "multipart/form-data" |
---|
29 | |
---|
30 | if callable(url): |
---|
31 | url = url() |
---|
32 | else: |
---|
33 | url = html_escape(url) |
---|
34 | |
---|
35 | method_tag = "" |
---|
36 | |
---|
37 | if method in ['post', 'get']: |
---|
38 | options['method'] = method |
---|
39 | else: |
---|
40 | options['method'] = "post" |
---|
41 | method_tag = tag('input', type="hidden", name_="_method", value=method) |
---|
42 | |
---|
43 | options["action"] = url |
---|
44 | return tag("form", True, **options) + method_tag |
---|
45 | |
---|
46 | start_form = form |
---|
47 | |
---|
48 | def end_form(): |
---|
49 | """ |
---|
50 | Outputs "</form>" |
---|
51 | """ |
---|
52 | return "</form>" |
---|
53 | |
---|
54 | def select(name, option_tags='', **options): |
---|
55 | """ |
---|
56 | Creates a dropdown selection box |
---|
57 | |
---|
58 | ``option_tags`` is a string containing the option tags for the select box:: |
---|
59 | |
---|
60 | >>> select("people", "<option>George</option>") |
---|
61 | <select id="people" name="people"><option>George</option></select> |
---|
62 | |
---|
63 | Options: |
---|
64 | |
---|
65 | * ``multiple`` - If set to true the selection will allow multiple choices. |
---|
66 | |
---|
67 | """ |
---|
68 | o = { 'name_': name, 'id': name } |
---|
69 | o.update(options) |
---|
70 | return content_tag("select", option_tags, **o) |
---|
71 | |
---|
72 | def text_field(name, value=None, **options): |
---|
73 | """ |
---|
74 | Creates a standard text field. |
---|
75 | |
---|
76 | ``value`` is a string that will the contents of the text field will be set to |
---|
77 | |
---|
78 | Options: |
---|
79 | |
---|
80 | * ``disabled`` - If set to True, the user will not be able to use this input. |
---|
81 | * ``size`` - The number of visible characters that will fit in the input. |
---|
82 | * ``maxlength`` - The maximum number of characters that the browser will allow the user to enter. |
---|
83 | |
---|
84 | Remaining keyword options will be standard HTML options for the tag. |
---|
85 | """ |
---|
86 | o = {'type': 'text', 'name_': name, 'id': name, 'value': value} |
---|
87 | o.update(options) |
---|
88 | return tag("input", **o) |
---|
89 | |
---|
90 | def hidden_field(name, value=None, **options): |
---|
91 | """ |
---|
92 | Creates a hidden field. |
---|
93 | |
---|
94 | Takes the same options as text_field |
---|
95 | """ |
---|
96 | return text_field(name, value, type="hidden", **options) |
---|
97 | |
---|
98 | def file_field(name, value=None, **options): |
---|
99 | """ |
---|
100 | Creates a file upload field. |
---|
101 | |
---|
102 | If you are using file uploads then you will also need to set the multipart option for the form. |
---|
103 | """ |
---|
104 | return text_field(name, value=value, type="file", **options) |
---|
105 | |
---|
106 | def password_field(name="password", value=None, **options): |
---|
107 | """ |
---|
108 | Creates a password field |
---|
109 | |
---|
110 | Takes the same options as text_field |
---|
111 | """ |
---|
112 | return text_field(name, value, type="password", **options) |
---|
113 | |
---|
114 | def text_area(name, content='', **options): |
---|
115 | """ |
---|
116 | Creates a text input area. |
---|
117 | |
---|
118 | Options: |
---|
119 | |
---|
120 | * ``size`` - A string specifying the dimensions of the textarea. |
---|
121 | |
---|
122 | Example:: |
---|
123 | |
---|
124 | >>> text_area("body", '', size="25x10") |
---|
125 | <textarea name="body" id="body" cols="25" rows="10"></textarea> |
---|
126 | """ |
---|
127 | if 'size' in options: |
---|
128 | options["cols"], options["rows"] = options["size"].split("x") |
---|
129 | del options['size'] |
---|
130 | o = {'name_': name, 'id': name} |
---|
131 | o.update(options) |
---|
132 | return content_tag("textarea", content, **o) |
---|
133 | |
---|
134 | def check_box(name, value="1", checked=False, **options): |
---|
135 | """ |
---|
136 | Creates a check box. |
---|
137 | """ |
---|
138 | o = {'type': 'checkbox', 'name_': name, 'id': name, 'value': value} |
---|
139 | o.update(options) |
---|
140 | if checked: |
---|
141 | o["checked"] = "checked" |
---|
142 | return tag("input", **o) |
---|
143 | |
---|
144 | def radio_button(name, value, checked=False, **options): |
---|
145 | """Creates a radio button.""" |
---|
146 | o = {'type': 'radio', 'name_': name, 'id': name, 'value': value} |
---|
147 | o.update(options) |
---|
148 | if checked: |
---|
149 | o["checked"] = "checked" |
---|
150 | return tag("input", **o) |
---|
151 | |
---|
152 | def submit(value="Save changes", name='commit', confirm=None, disable_with=None, **options): |
---|
153 | """Creates a submit button with the text ``value`` as the caption. |
---|
154 | |
---|
155 | Options: |
---|
156 | |
---|
157 | * ``confirm`` - A confirm message displayed when the button is clicked. |
---|
158 | * ``disable_with`` - The value to be used to rename a disabled version of the submit |
---|
159 | button. |
---|
160 | """ |
---|
161 | |
---|
162 | if confirm: |
---|
163 | onclick = options.get('onclick', '') |
---|
164 | if onclick.strip() and not onclick.rstrip().endswith(';'): |
---|
165 | onclick += ';' |
---|
166 | options['onclick'] = "%s return %s;" % (onclick, confirm_javascript_function(confirm)) |
---|
167 | |
---|
168 | if disable_with: |
---|
169 | options["onclick"] = "this.disabled=true;this.value='%s';this.form.submit();%s" % (disable_with, options.get("onclick", '')) |
---|
170 | o = {'type': 'submit', 'name_': name, 'value': value } |
---|
171 | o.update(options) |
---|
172 | return tag("input", **o) |
---|
173 | |
---|
174 | #def image_submit(source, **options): |
---|
175 | # """Displays an image which when clicked will submit the form""" |
---|
176 | # o = {'type': 'image', 'src': image_path_source) } |
---|
177 | # o.update(options) |
---|
178 | # return tag("input", **o) |
---|
179 | |
---|
180 | __all__ = ['form', 'start_form', 'end_form', 'select', 'text_field', 'hidden_field', 'file_field', |
---|
181 | 'password_field', 'text_area', 'check_box', 'radio_button', 'submit'] |
---|