r26402 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26401‎ | r26402 | r26403 >
Date:15:41, 4 October 2007
Author:evan
Status:old
Tags:
Comment:
Handle kind-of correctly required fields.
Modified paths:
  • /trunk/extensions/SpecialForm/README (modified) (history)
  • /trunk/extensions/SpecialForm/SpecialForm.body.php (modified) (history)
  • /trunk/extensions/SpecialForm/SpecialForm.i18n.php (modified) (history)
  • /trunk/extensions/SpecialForm/TODO (modified) (history)

Diff [purge]

Index: trunk/extensions/SpecialForm/TODO
@@ -2,7 +2,7 @@
33 - add a token to session, store it in the form, and check that they
44 match on submit, to prevent automated creation
55 + create a new article on submit
6 -- required/optional fields
 6++ required/optional fields
77 + check for existing article before saving
88 + add a default value if passed in using GET
99 + test setting custom title in form description
Index: trunk/extensions/SpecialForm/SpecialForm.body.php
@@ -90,7 +90,7 @@
9191 return new Form($name, $text);
9292 }
9393
94 - function showForm($form) {
 94+ function showForm($form, $errmsg = NULL) {
9595 global $wgOut, $wgRequest, $wgParser, $wgTitle;
9696
9797 $self = SpecialPage::getTitleFor("Form/$form->name");
@@ -105,6 +105,13 @@
106106 wfElement('br'));
107107 }
108108
 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+
109116 $wgOut->addHtml(wfOpenElement('form',
110117 array('method' => 'POST',
111118 'action' => $self->getLocalURL())));
@@ -121,14 +128,42 @@
122129
123130 function createArticle($form) {
124131
125 - global $wgOut, $wgRequest;
 132+ global $wgOut, $wgRequest, $wgLang;
 133+
 134+ # Check for required fields
126135
 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+
127157 $title = $this->makeTitle($form);
128158
129159 wfDebug("SpecialForm: saving article '$title'\n");
130160
131161 $nt = Title::newFromText($title);
132162
 163+ if (!$nt) {
 164+ $wgOut->showErrorPage('formbadpagename', 'formbadpagenametext', array($title));
 165+ return;
 166+ }
 167+
133168 if ($nt->getArticleID() != 0) {
134169 $wgOut->showErrorPage('formarticleexists', 'formarticleexists', array($title));
135170 return;
@@ -137,7 +172,6 @@
138173 $text = "{{subst:$form->template";
139174
140175 foreach ($form->fields as $name => $field) {
141 - # XXX: check for required fields
142176 # FIXME: strip/escape template-related chars (|, =, }})
143177 $text .= "|$name=" . $wgRequest->getText($name);
144178 }
@@ -275,6 +309,14 @@
276310 }
277311 }
278312
 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+
279321 function render($def = NULL) {
280322 global $wgOut;
281323
Index: trunk/extensions/SpecialForm/SpecialForm.i18n.php
@@ -38,6 +38,10 @@
3939 'formsave' => 'Save',
4040 'formarticleexists' => 'Page Exists',
4141 '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.',
4246 'formsavesummary' => 'New page using [[Special:Form/$1]]',
4347 'formsaveerror' => 'Error Saving Form',
4448 'formsaveerrortext' => 'There was an unknown error saving page \'$1\'.',
Index: trunk/extensions/SpecialForm/README
@@ -174,6 +174,11 @@
175175 example, "Blue;Green;Red" will give the user a drop-down box with
176176 three choices, "Blue", "Green", and "Red".
177177
 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+
178183 == Using forms ==
179184
180185 To show a user a form, use the following special-page format:

Status & tagging log