You are here: Foswiki>Tasks Web>Item12441 (19 Jan 2017, MichaelDaum)Edit Attach

Item12441: Simple RegExp Implementation for uncommon data and more variable Form validation

pencil
Priority: Enhancement
Current State: Closed
Released In: n/a
Target Release: n/a
Applies To: Extension
Component: FormPlugin
Branches:
Reported By: RobertFelber
Waiting For:
Last Change By: MichaelDaum
I saw no direct way to validate uncommon data (such as roomnumbers, printer names ...) in terms of FormPlugin -validation. Regular expressions and methods are encapsulated (good practise), but it would be nice to have some more flexible tool. It is not a big feature but it would ease work a lot.

Description

For the FormPlugin there could be a new method for validation called "regexp" which gets a simple regular expression as parameter. It should also not destroy already implemented validation mechanisms/cause security issues.

Example

...
%FORMELEMENT{
   name="roomnr"
   title="Roomnumber"
   type="text"
   value=""
   validate="{
      rules: {
         regexpr : '/^[(Aa)|(Bb)|(Cc)][1-9][1-9][1-9]$/'
      },
      messages: {
         regexpr : 'Roomnumbers start with A,B or C, followed by 3 numbers.'
      }
   }" 
}%
...

Implementation

Here is a list of things I changed and how it worked for me (installation out of the debian repo):

in ../lib/Foswiki/Plugins/FormPlugin/Validation/BackendValidator.pm add:
sub validate_regexpr {
    my ( $name , $value , $conditionalValue ) = @_;
    my $isValid = '';
    $conditionalValue =~ s/^\///g;  # just that it looks familiar
    $conditionalValue =~ s/\/$//g;  #
    $conditionalValue =~ s/&vbar;/\|/g;  #replace &vbar; by |
    #
    # better check of input, propably by eval()?
    #
    if ( $conditionalValue ) {
        try {
            $isValid = ( $value =~ m/($conditionalValue)/ );
        }
        catch Error with {
            my $e = shift;
            Foswiki::Func::writeDebug( "validate_regexpr ", $e, " had a problem" );
            Foswiki::Func::writeDebug( "regExString:" , $conditionalValue);
        };
    }
    my $message =
      $isValid
      ? ''
      : Foswiki::Plugins::FormPlugin::Validate::ValidationInstruction::parseMessage(
        $name);
    return ( $isValid, $message );
}

in ../lib/Foswiki/FormPlugin/Constants.pm:
our $ERROR_MESSAGES = {
    required    => "This field is required.",
    remote      => "Please fix this field.",
    email       => "Please enter a valid email address.",
    multiemail  => "Please enter one or more valid email addresses.",
    url         => "Please enter a valid URL.",
    date        => "Please enter a valid date.",
    dateISO     => "Please enter a valid date (ISO).",
    number      => "Please enter a valid number.",
    integer     => "Please enter a rounded number.",
    float       => "Please enter a fractional number.",
    digits      => "Please enter only digits.",
    creditcard  => "Please enter a valid credit card number.",
    equalTo     => "Please enter the same value again.",
    accept      => "Please enter a value with a valid extension.",
    maxlength   => "Please enter no more than {0} characters.",
    minlength   => "Please enter at least {0} characters.",
    rangelength => "Please enter a value between {0} and {1} characters long.",
    range       => "Please enter a value between {0} and {1}.",
    max         => "Please enter a value less than or equal to {0}.",
    min         => "Please enter a value greater than or equal to {0}.",
    wikiword    => "Please enter a <nop>WikiWord.",
    regexpr     => "Value did not match pattern"
};

in ../foswiki/pub/System/JQueryPlugin/plugins/validate/jquery.validate.methods and jquery.validate.foswiki-methods.uncompressed.js add:
jQuery.validator.addMethod(
    "regexpr", 
    function(value, element, param) {
    //param comes as string / better check for errors?
      var p = new RegExp(param);
     return this.optional(element) || p.test(value);
    }, 
    "Invalid format."
);

-- RobertFelber - 18 Mar 2013

Michael, Can you check out these enhancements? Can they be applied to the FormPlugin and JQueryPlugin?

-- GeorgeClark - 24 Dec 2014

Adding method "regexpr" to jquery.validate.foswiki-methods.uncompressed.js should be straight forward. Though I don't have a test to verify it is working correctly.

Can't speak for FormPlugin as I never used it.

-- MichaelDaum - 07 Jan 2015

Removing JQueryPlugin from the list of components. This bug item is requiring actions. Alas, mentioned people have left the project.

-- MichaelDaum - 19 Jan 2017
 

ItemTemplate edit

Summary Simple RegExp Implementation for uncommon data and more variable Form validation
ReportedBy RobertFelber
Codebase
SVN Range
AppliesTo Extension
Component FormPlugin
Priority Enhancement
CurrentState Closed
WaitingFor
Checkins
TargetRelease n/a
ReleasedIn n/a
CheckinsOnBranches
trunkCheckins
masterCheckins
ItemBranchCheckins
Release02x01Checkins
Release02x00Checkins
Release01x01Checkins
Topic revision: r5 - 19 Jan 2017, MichaelDaum
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy