FormValidation

Summary: How to validate forms within wiki pages
Version:
Prerequisites:
Status:
Maintainer: Luigi -> mailto:kirpi [snail] kirpi [period] it
Categories: Forms

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

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)

  1. I use Fox and I had to modify one line in the FoxFormMarkup function. Replace:
    return FmtPageName("<form action='\$PageUrl' method='post'>
with:
return FmtPageName("<form name='$name' action='\$PageUrl' method='post'>
Make sure your form recipe also insert form's name in the HTML code.
  1. Add the (:html:), (:htmlend:) custom markup as described in the Javascript recipe.
  2. Download the gen_validatorv2.js script from JavaScript-Coder.com and copy it somewhere in the uploads folder.
  3. 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>";
Replace "mydomain" with your domain...
  1. 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:)
    
CheckRadioButton is a custom function to validate Radio Button. You can add as many frmvalidator.addValidation() line as you wish. frmvalidator.setAddnlValidationFunction() just register the CheckRadioButton function.

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:


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?