Skip to content

Commit 6edfb91

Browse files
authored
[9.x] Add validation rules: ascii_alpha, ascii_alpha_num, ascii_alpha_dash (#45769)
* add ascii validation rules * add test * refactor rules * fix tests * fix docs
1 parent 8c04967 commit 6edfb91

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,45 +339,61 @@ protected function getDateTime($value)
339339

340340
/**
341341
* Validate that an attribute contains only alphabetic characters.
342+
* If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters.
342343
*
343344
* @param string $attribute
344345
* @param mixed $value
345346
* @return bool
346347
*/
347-
public function validateAlpha($attribute, $value)
348+
public function validateAlpha($attribute, $value, $parameters)
348349
{
350+
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
351+
return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value);
352+
}
353+
349354
return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value);
350355
}
351356

352357
/**
353358
* Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
359+
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters,
360+
* dashes, and underscores.
354361
*
355362
* @param string $attribute
356363
* @param mixed $value
357364
* @return bool
358365
*/
359-
public function validateAlphaDash($attribute, $value)
366+
public function validateAlphaDash($attribute, $value, $parameters)
360367
{
361368
if (! is_string($value) && ! is_numeric($value)) {
362369
return false;
363370
}
364371

372+
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
373+
return preg_match('/^[a-zA-Z0-9_-]+$/u', $value) > 0;
374+
}
375+
365376
return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
366377
}
367378

368379
/**
369380
* Validate that an attribute contains only alpha-numeric characters.
381+
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters.
370382
*
371383
* @param string $attribute
372384
* @param mixed $value
373385
* @return bool
374386
*/
375-
public function validateAlphaNum($attribute, $value)
387+
public function validateAlphaNum($attribute, $value, $parameters)
376388
{
377389
if (! is_string($value) && ! is_numeric($value)) {
378390
return false;
379391
}
380392

393+
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
394+
return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0;
395+
}
396+
381397
return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0;
382398
}
383399

tests/Validation/ValidationValidatorTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4584,6 +4584,95 @@ public function testValidateAlphaDash()
45844584
$this->assertTrue($v->passes());
45854585
}
45864586

4587+
public function testValidateAlphaWithAsciiOption()
4588+
{
4589+
$trans = $this->getIlluminateArrayTranslator();
4590+
$v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha:ascii']);
4591+
$this->assertTrue($v->passes());
4592+
4593+
$trans = $this->getIlluminateArrayTranslator();
4594+
$v = new Validator($trans, [
4595+
'x' => 'aslsdlks
4596+
1
4597+
1',
4598+
], ['x' => 'Alpha:ascii']);
4599+
$this->assertFalse($v->passes());
4600+
4601+
$v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'Alpha:ascii']);
4602+
$this->assertFalse($v->passes());
4603+
4604+
$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'Alpha:ascii']);
4605+
$this->assertFalse($v->passes());
4606+
4607+
$v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'Alpha:ascii']);
4608+
$this->assertFalse($v->passes());
4609+
4610+
$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'Alpha:ascii']);
4611+
$this->assertFalse($v->passes());
4612+
4613+
$v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'Alpha:ascii']);
4614+
$this->assertFalse($v->passes());
4615+
4616+
$v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'Alpha:ascii']);
4617+
$this->assertFalse($v->passes());
4618+
4619+
$v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'Alpha:ascii']);
4620+
$this->assertFalse($v->passes());
4621+
4622+
$v = new Validator($trans, ['x' => ''], ['x' => 'Alpha:ascii']);
4623+
$this->assertFalse($v->passes());
4624+
4625+
$v = new Validator($trans, ['x' => '123'], ['x' => 'Alpha:ascii']);
4626+
$this->assertFalse($v->passes());
4627+
4628+
$v = new Validator($trans, ['x' => 123], ['x' => 'Alpha:ascii']);
4629+
$this->assertFalse($v->passes());
4630+
4631+
$v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha:ascii']);
4632+
$this->assertFalse($v->passes());
4633+
}
4634+
4635+
public function testValidateAlphaNumWithAsciiOption()
4636+
{
4637+
$trans = $this->getIlluminateArrayTranslator();
4638+
$v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum:ascii']);
4639+
$this->assertTrue($v->passes());
4640+
4641+
$v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AlphaNum:ascii']);
4642+
$this->assertFalse($v->passes());
4643+
4644+
$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と123'], ['x' => 'AlphaNum:ascii']);
4645+
$this->assertFalse($v->passes());
4646+
4647+
$v = new Validator($trans, ['x' => '१२३'], ['x' => 'AlphaNum:ascii']); // numbers in Hindi
4648+
$this->assertFalse($v->passes());
4649+
4650+
$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaNum:ascii']); // eastern arabic numerals
4651+
$this->assertFalse($v->passes());
4652+
4653+
$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum:ascii']);
4654+
$this->assertFalse($v->passes());
4655+
}
4656+
4657+
public function testValidateAlphaDashWithAsciiOption()
4658+
{
4659+
$trans = $this->getIlluminateArrayTranslator();
4660+
$v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash:ascii']);
4661+
$this->assertTrue($v->passes());
4662+
4663+
$v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AlphaDash:ascii']);
4664+
$this->assertFalse($v->passes());
4665+
4666+
$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と-_123'], ['x' => 'AlphaDash:ascii']);
4667+
$this->assertFalse($v->passes());
4668+
4669+
$v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AlphaDash:ascii']);
4670+
$this->assertFalse($v->passes());
4671+
4672+
$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash:ascii']); // eastern arabic numerals
4673+
$this->assertFalse($v->passes());
4674+
}
4675+
45874676
public function testValidateTimezone()
45884677
{
45894678
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)