FormValidation
Question
How can I validate forms within wiki pages?
Answer
Real form validation is to be done server-side. Nevertheless client-side validation is still quite useful: you could include a tiny javascript within your pages.
Notes
- Including javascript in wiki pages is dangerous.
- Pm suggests that an ad hoc recipe could be written
- Ideas on how to include form validation scripts could easily be found anywhere on the net: if you have any advice, though, please post it here.
Comments
I am already successfully using a slightly extended version of the scripts/forms.php. It supports a new "validateinput" action that takes _regex and _message arguments passed to (:input:) fields, constructs the $MessagesFmt for invalid fields and focuses the first invalid field via javascript when form validation fails. When a submission is completely valid, it calls a user-defineable function with the form name and the field values. In my environment, that function stores the fields in another page and sends an email with the values.
jm February 13, 2006, at 05:53 PM
See the article on my personal PmWiki for more details, an example and download.
Jm March 22, 2006, at 02:07 AM
Here is a recipe
You can implement form validation using the Javascript script described at JavaScript-Coder.com. (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml)
- I use Fox and I had to modify one line in the FoxFormMarkup function. Replace:
return FmtPageName("<form action='\$PageUrl' method='post'>
- Add the (:html:), (:htmlend:) custom markup as described in the Javascript recipe.
- Download the gen_validatorv2.js script from JavaScript-Coder.com and copy it somewhere in the uploads folder.
- Create a custom Group.NameOfThePageContainingYourForm.php so this script will be included only in the page containing your form. Put the following code in it:
<?php if (!defined('PmWiki')) exit(); $HTMLHeaderFmt['analytics'] = "\n<script src='http://www.mydomain.com/uploads/gen_validatorv2.js' type='text/javascript'></script>";
- You can then add code like this after your form:
(:html:) <script language="JavaScript"> function CheckRadioButton() { var frm = document.forms["formname"]; if (!frm.radiofieldname[0].checked && !frm.radiofieldname[1].checked) { alert("Some warning..."); return false; } return true; } var frmvalidator = new Validator("formname"); frmvalidator.addValidation("fieldname","req","Some warning..."); frmvalidator.setAddnlValidationFunction("CheckRadioButton"); </script> (:htmlend:)
Pierre Racine, Feb, 2007
WikiSh provides a pretty rich capability for form validation on the server side. See in particular the {(wikish_form QUICKFORM PROCESS)}
or just {(wikish_form PROCESS)}
to get the form started and the variables available within WikiSh context. Then it's a simple matter of programming what you want it to do in terms of validating.
Peter Bowers March 22, 2008, at 06:57 PM
See the following links for examples:
- WikiShExamples#DataCollect (Validate form and collect data)
- WikiShExamples#ButtonLink (Redirect to another page based on values in form)
- WikiShExamples#PagesFromTemplate (Create pages with names based on values from form)
wForms input validation
If you're looking for a hardened secure javascript solution to use, try the free wForms libs from formassembly; [(approve links) edit diff] which I've been using a lot (outside and inside of pmwiki) and has all you need for client-side validation, plus a lot more. Especially if the user already limits access to the pages you use this on, it's pretty darn secure. Julius Thyssen April 2008
See Also
Contributors
- Luigi -> mailto:kirpi [snail] kirpi [period] it
Comments
See discussion at FormValidation-Talk?