Priority: Enhancement
Current State: Closed
Released In: n/a
Target Release: n/a
Applies To: Extension
Component: FormPlugin
Branches:
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