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
3 changes: 2 additions & 1 deletion lib/Validator/LIVR/Rules/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public static function lengthBetween($minLength, $maxLength)

public static function like($re)
{
$re = '/' . $re . '/';
// D: $ matches only at the end of the value, as in JavaScript, not before a trailing newline
$re = '/' . $re . '/D';

if (func_num_args() == 3) { #Passed regexp flag
$flags = func_get_arg(1);
Expand Down
19 changes: 19 additions & 0 deletions tests/05-rules_edge_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ public function testEmptyListsOfRules()
'Should skip fields without rules'
);
}

public function testLikeMatchesTheEndOfTheValue()
{
$validator = new Validator\LIVR(array(
'zip1' => array('like' => '^[0-9]{5}$'),
'zip2' => array('like' => array('^[0-9]{5}$', 'i')),
'zip3' => array('like' => '^[0-9]{5}$'),
));

$this->assertFalse(
$validator->validate(array('zip1' => "12345\n", 'zip2' => "12345\n", 'zip3' => '12345')),
'Should return false'
);
$this->assertEquals(
array('zip1' => 'WRONG_FORMAT', 'zip2' => 'WRONG_FORMAT'),
$validator->getErrors(),
'Should not match $ before a trailing newline'
);
}
}
Loading