Skip to content

Commit 1a42e08

Browse files
authored
Fix ValidationValidator not to accept terminating newline (#45790)
* Add test for do not accepting ends with newline * Use \A and \z instead of ^ and $ for preg_match() to avoid last newline matches
1 parent b685a7a commit 1a42e08

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ protected function getDateTime($value)
348348
public function validateAlpha($attribute, $value, $parameters)
349349
{
350350
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
351-
return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value);
351+
return is_string($value) && preg_match('/\A[a-zA-Z]+\z/u', $value);
352352
}
353353

354-
return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value);
354+
return is_string($value) && preg_match('/\A[\pL\pM]+\z/u', $value);
355355
}
356356

357357
/**
@@ -370,10 +370,10 @@ public function validateAlphaDash($attribute, $value, $parameters)
370370
}
371371

372372
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
373-
return preg_match('/^[a-zA-Z0-9_-]+$/u', $value) > 0;
373+
return preg_match('/\A[a-zA-Z0-9_-]+\z/u', $value) > 0;
374374
}
375375

376-
return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
376+
return preg_match('/\A[\pL\pM\pN_-]+\z/u', $value) > 0;
377377
}
378378

379379
/**
@@ -391,10 +391,10 @@ public function validateAlphaNum($attribute, $value, $parameters)
391391
}
392392

393393
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
394-
return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0;
394+
return preg_match('/\A[a-zA-Z0-9]+\z/u', $value) > 0;
395395
}
396396

397-
return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0;
397+
return preg_match('/\A[\pL\pM\pN]+\z/u', $value) > 0;
398398
}
399399

400400
/**

tests/Validation/ValidationValidatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4547,6 +4547,9 @@ public function testValidateAlpha()
45474547

45484548
$v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha']);
45494549
$this->assertFalse($v->passes());
4550+
4551+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'Alpha']); // ends with newline
4552+
$this->assertFalse($v->passes());
45504553
}
45514554

45524555
public function testValidateAlphaNum()
@@ -4566,6 +4569,9 @@ public function testValidateAlphaNum()
45664569

45674570
$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum']);
45684571
$this->assertTrue($v->passes());
4572+
4573+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'AlphaNum']); // ends with newline
4574+
$this->assertFalse($v->passes());
45694575
}
45704576

45714577
public function testValidateAlphaDash()
@@ -4582,6 +4588,9 @@ public function testValidateAlphaDash()
45824588

45834589
$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash']); // eastern arabic numerals
45844590
$this->assertTrue($v->passes());
4591+
4592+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'AlphaDash']); // ends with newline
4593+
$this->assertFalse($v->passes());
45854594
}
45864595

45874596
public function testValidateAlphaWithAsciiOption()
@@ -4630,6 +4639,9 @@ public function testValidateAlphaWithAsciiOption()
46304639

46314640
$v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha:ascii']);
46324641
$this->assertFalse($v->passes());
4642+
4643+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'Alpha:ascii']); // ends with newline
4644+
$this->assertFalse($v->passes());
46334645
}
46344646

46354647
public function testValidateAlphaNumWithAsciiOption()
@@ -4652,6 +4664,9 @@ public function testValidateAlphaNumWithAsciiOption()
46524664

46534665
$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum:ascii']);
46544666
$this->assertFalse($v->passes());
4667+
4668+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'AlphaNum:ascii']); // ends with newline
4669+
$this->assertFalse($v->passes());
46554670
}
46564671

46574672
public function testValidateAlphaDashWithAsciiOption()
@@ -4671,6 +4686,9 @@ public function testValidateAlphaDashWithAsciiOption()
46714686

46724687
$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash:ascii']); // eastern arabic numerals
46734688
$this->assertFalse($v->passes());
4689+
4690+
$v = new Validator($trans, ['x' => "abc\n"], ['x' => 'AlphaDash:ascii']); // ends with newline
4691+
$this->assertFalse($v->passes());
46744692
}
46754693

46764694
public function testValidateTimezone()

0 commit comments

Comments
 (0)