Skip to content

Commit

Permalink
add Form::PATTERN_ICASE as a case-insensitive variant of Form::PATTERN;
Browse files Browse the repository at this point in the history
fixes nette#185
  • Loading branch information
renekliment committed Jul 25, 2018
1 parent 5613893 commit dd41d13
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Form extends Container implements Nette\Utils\IHtmlString
EMAIL = ':email',
URL = ':url',
PATTERN = ':pattern',
PATTERN_ICASE = ':patternCaseInsensitive',
INTEGER = ':integer',
NUMERIC = ':numeric',
FLOAT = ':float',
Expand Down
12 changes: 8 additions & 4 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,20 @@ public static function validateUrl(IControl $control): bool
return false;
}


/**
* Matches control's value regular expression?
* Does the control's value match the regular expression?
* Case-sensitive to comply with the HTML5 <input /> pattern attribute behaviour
*/
public static function validatePattern(IControl $control, string $pattern): bool
public static function validatePattern(IControl $control, string $pattern, string $modifiers = 'u'): bool
{
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u");
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01" . $modifiers);
}

public static function validatePatternCaseInsensitive(IControl $control, string $pattern): bool
{
return self::validatePattern($control, $pattern, 'ui');
}

/**
* Is a control's value numeric?
Expand Down
9 changes: 7 additions & 2 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,14 @@
} catch (e) {} // eslint-disable-line no-empty
},

pattern: function(elem, arg, val) {
pattern: function(elem, arg, val, value, modifiers) {
if (typeof arg !== 'string') {
return null;
}

try {
var regExp = new RegExp('^(?:' + arg + ')$');
modifiers = modifiers || undefined;
var regExp = new RegExp('^(?:' + arg + ')$', modifiers);

if (window.FileList && val instanceof FileList) {
for (var i = 0; i < val.length; i++) {
Expand All @@ -470,6 +471,10 @@
} catch (e) {} // eslint-disable-line no-empty
},

patternCaseInsensitive: function(elem, arg, val) {
return Nette.validators.pattern(elem, arg, val, null, 'i');
},

numeric: function(elem, arg, val) {
if (elem.type === 'number' && elem.validity.badInput) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions tests/Forms/Controls.TestBase.validators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ test(function () {
Assert::false(Validator::validatePattern($control, '[0-9]+X'));
});

test(function () {
$control = new TextInput;
$control->value = '123x';
Assert::false(Validator::validatePatternCaseInsensitive($control, '[0-9]'));
Assert::true(Validator::validatePatternCaseInsensitive($control, '[0-9]+x'));
Assert::true(Validator::validatePatternCaseInsensitive($control, '[0-9]+X'));
});


test(function () {
class MockUploadControl extends UploadControl
Expand Down
8 changes: 8 additions & 0 deletions tests/netteForms/spec/Nette.validateRuleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('Nette.getValue & validateRule', function() {
expect(Nette.validateRule(el, 'pattern', '\\d')).toBe(false);
expect(Nette.validateRule(el, 'pattern', '\\w')).toBe(false);
expect(Nette.validateRule(el, 'pattern', '\\w+')).toBe(true);
expect(Nette.validateRule(el, 'pattern', 'hello')).toBe(true);
expect(Nette.validateRule(el, 'pattern', 'HELLO')).toBe(false);
expect(Nette.validateRule(el, 'patternCaseInsensitive', '\\d+')).toBe(false);
expect(Nette.validateRule(el, 'patternCaseInsensitive', '\\d')).toBe(false);
expect(Nette.validateRule(el, 'patternCaseInsensitive', '\\w')).toBe(false);
expect(Nette.validateRule(el, 'patternCaseInsensitive', '\\w+')).toBe(true);
expect(Nette.validateRule(el, 'patternCaseInsensitive', 'hello')).toBe(true);
expect(Nette.validateRule(el, 'patternCaseInsensitive', 'HELLO')).toBe(true);
expect(Nette.validateRule(el, 'integer')).toBe(false);
expect(Nette.validateRule(el, 'float')).toBe(false);

Expand Down

0 comments on commit dd41d13

Please sign in to comment.