Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 11 additions & 3 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,20 @@ public static function validateUrl(IControl $control): bool


/**
* 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, bool $caseInsensitive = false): bool
{
$value = $control->getValue() instanceof Nette\Http\FileUpload ? $control->getValue()->getName() : $control->getValue();
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01u");
$modifiers = 'u' . ($caseInsensitive ? 'i' : '');
return (bool) Strings::match($value, "\x01^(?:$pattern)\\z\x01" . $modifiers);
}


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


Expand Down
11 changes: 8 additions & 3 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,17 @@
} catch (e) {} // eslint-disable-line no-empty
},

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

var extraModifiers = (caseInsensitive ? 'i' : '');
try {
try {
var regExp = new RegExp('^(?:' + arg + ')$', 'u');
var regExp = new RegExp('^(?:' + arg + ')$', 'u' + extraModifiers);
} catch (e) {
regExp = new RegExp('^(?:' + arg + ')$');
regExp = new RegExp('^(?:' + arg + ')$', extraModifiers);
}

if (window.FileList && val instanceof FileList) {
Expand All @@ -474,6 +475,10 @@
} catch (e) {} // eslint-disable-line no-empty
},

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

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