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
22 changes: 19 additions & 3 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,45 +338,61 @@ protected function getDateTime($value)

/**
* Validate that an attribute contains only alphabetic characters.
* If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateAlpha($attribute, $value)
public function validateAlpha($attribute, $value, $parameters)
{
if (isset($parameters[0]) && $parameters[0] === 'ascii') {
return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value);
}

return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value);
}

/**
* Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters,
* dashes, and underscores.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateAlphaDash($attribute, $value)
public function validateAlphaDash($attribute, $value, $parameters)
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
}

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

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

/**
* Validate that an attribute contains only alpha-numeric characters.
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateAlphaNum($attribute, $value)
public function validateAlphaNum($attribute, $value, $parameters)
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
}

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

return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0;
}

Expand Down
89 changes: 89 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4497,6 +4497,95 @@ public function testValidateAlphaDash()
$this->assertTrue($v->passes());
}

public function testValidateAlphaWithAsciiOption()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha:ascii']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [
'x' => 'aslsdlks
1
1',
], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => '❤'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => '123'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 123], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha:ascii']);
$this->assertFalse($v->passes());
}

public function testValidateAlphaNumWithAsciiOption()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum:ascii']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AlphaNum:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と123'], ['x' => 'AlphaNum:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => '१२३'], ['x' => 'AlphaNum:ascii']); // numbers in Hindi
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaNum:ascii']); // eastern arabic numerals
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum:ascii']);
$this->assertFalse($v->passes());
}

public function testValidateAlphaDashWithAsciiOption()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash:ascii']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AlphaDash:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'ユニコードを基盤技術と-_123'], ['x' => 'AlphaDash:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AlphaDash:ascii']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash:ascii']); // eastern arabic numerals
$this->assertFalse($v->passes());
}

public function testValidateTimezone()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down