Index: trunk/extensions/SpecialForm/TODO |
— | — | @@ -2,7 +2,7 @@ |
3 | 3 | - add a token to session, store it in the form, and check that they |
4 | 4 | match on submit, to prevent automated creation |
5 | 5 | + create a new article on submit |
6 | | -- required/optional fields |
| 6 | ++ required/optional fields |
7 | 7 | + check for existing article before saving |
8 | 8 | + add a default value if passed in using GET |
9 | 9 | + test setting custom title in form description |
Index: trunk/extensions/SpecialForm/SpecialForm.body.php |
— | — | @@ -90,7 +90,7 @@ |
91 | 91 | return new Form($name, $text); |
92 | 92 | } |
93 | 93 | |
94 | | - function showForm($form) { |
| 94 | + function showForm($form, $errmsg = NULL) { |
95 | 95 | global $wgOut, $wgRequest, $wgParser, $wgTitle; |
96 | 96 | |
97 | 97 | $self = SpecialPage::getTitleFor("Form/$form->name"); |
— | — | @@ -105,6 +105,13 @@ |
106 | 106 | wfElement('br')); |
107 | 107 | } |
108 | 108 | |
| 109 | + if (!is_null($errmsg)) { |
| 110 | + $wgOut->addHtml(wfOpenElement('div', array('class' => 'error')) . |
| 111 | + $wgOut->parse($errmsg) . |
| 112 | + wfCloseElement('div') . |
| 113 | + wfElement('br')); |
| 114 | + } |
| 115 | + |
109 | 116 | $wgOut->addHtml(wfOpenElement('form', |
110 | 117 | array('method' => 'POST', |
111 | 118 | 'action' => $self->getLocalURL()))); |
— | — | @@ -121,14 +128,42 @@ |
122 | 129 | |
123 | 130 | function createArticle($form) { |
124 | 131 | |
125 | | - global $wgOut, $wgRequest; |
| 132 | + global $wgOut, $wgRequest, $wgLang; |
| 133 | + |
| 134 | + # Check for required fields |
126 | 135 | |
| 136 | + $missedFields = array(); |
| 137 | + |
| 138 | + foreach ($form->fields as $name => $field) { |
| 139 | + $value = $wgRequest->getText($name); |
| 140 | + if ($field->isOptionTrue('required') && (is_null($value) || strlen($value) == 0)) { |
| 141 | + $missedFields[] = $field->label; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + # On error, show the form again with some error text. |
| 146 | + |
| 147 | + if ($missedFields) { |
| 148 | + if (count($missedFields) > 1) { |
| 149 | + $msg = wfMsg('formrequiredfieldpluralerror', $wgLang->listToText($missedFields)); |
| 150 | + } else { |
| 151 | + $msg = wfMsg('formrequiredfielderror', $missedFields[0]); |
| 152 | + } |
| 153 | + $this->showForm($form, $msg); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
127 | 157 | $title = $this->makeTitle($form); |
128 | 158 | |
129 | 159 | wfDebug("SpecialForm: saving article '$title'\n"); |
130 | 160 | |
131 | 161 | $nt = Title::newFromText($title); |
132 | 162 | |
| 163 | + if (!$nt) { |
| 164 | + $wgOut->showErrorPage('formbadpagename', 'formbadpagenametext', array($title)); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
133 | 168 | if ($nt->getArticleID() != 0) { |
134 | 169 | $wgOut->showErrorPage('formarticleexists', 'formarticleexists', array($title)); |
135 | 170 | return; |
— | — | @@ -137,7 +172,6 @@ |
138 | 173 | $text = "{{subst:$form->template"; |
139 | 174 | |
140 | 175 | foreach ($form->fields as $name => $field) { |
141 | | - # XXX: check for required fields |
142 | 176 | # FIXME: strip/escape template-related chars (|, =, }}) |
143 | 177 | $text .= "|$name=" . $wgRequest->getText($name); |
144 | 178 | } |
— | — | @@ -275,6 +309,14 @@ |
276 | 310 | } |
277 | 311 | } |
278 | 312 | |
| 313 | + function isOptionTrue($key, $default = false) { |
| 314 | + $value = $this->getOption($key, $default); |
| 315 | + return ((strcasecmp($value, 'on') == 0) || |
| 316 | + (strcasecmp($value, 'yes') == 0) || |
| 317 | + (strcasecmp($value, 'true') == 0) || |
| 318 | + (strcasecmp($value, '1') == 0)); |
| 319 | + } |
| 320 | + |
279 | 321 | function render($def = NULL) { |
280 | 322 | global $wgOut; |
281 | 323 | |
Index: trunk/extensions/SpecialForm/SpecialForm.i18n.php |
— | — | @@ -38,6 +38,10 @@ |
39 | 39 | 'formsave' => 'Save', |
40 | 40 | 'formarticleexists' => 'Page Exists', |
41 | 41 | 'formarticleexiststext' => 'The page [[$1]] already exists.', |
| 42 | + 'formbadpagename' => 'Bad Page Name', |
| 43 | + 'formbadpagenametext' => 'The form data you entered makes a bad page name, "$1".', |
| 44 | + 'formrequiredfieldpluralerror' => 'The field $1 are required for this form. Please fill them in.', |
| 45 | + 'formrequiredfielderror' => 'The field $1 is required for this form. Please fill it in.', |
42 | 46 | 'formsavesummary' => 'New page using [[Special:Form/$1]]', |
43 | 47 | 'formsaveerror' => 'Error Saving Form', |
44 | 48 | 'formsaveerrortext' => 'There was an unknown error saving page \'$1\'.', |
Index: trunk/extensions/SpecialForm/README |
— | — | @@ -174,6 +174,11 @@ |
175 | 175 | example, "Blue;Green;Red" will give the user a drop-down box with |
176 | 176 | three choices, "Blue", "Green", and "Red". |
177 | 177 | |
| 178 | +All field types support the option 'required'. If this option is 'on', |
| 179 | +'true', 'yes', or '1', saving the form without that field filled in |
| 180 | +will result in an error (and the user is given a chance to enter the |
| 181 | +data again). |
| 182 | + |
178 | 183 | == Using forms == |
179 | 184 | |
180 | 185 | To show a user a form, use the following special-page format: |