Skip to content

Commit

Permalink
Add Alpha Channel support to Hex validation rule (#49069)
Browse files Browse the repository at this point in the history
* Support Alpha hexadecimal

* Add more supporting tests
  • Loading branch information
ahinkle authored Nov 21, 2023
1 parent 276d6eb commit 6a68969
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ public function validateUppercase($attribute, $value, $parameters)
*/
public function validateHexColor($attribute, $value)
{
return preg_match('/^#(?:[0-9a-fA-F]{3}){1,2}$/', $value) === 1;
return preg_match('/^#(?:[0-9a-fA-F]{3}){1,2}(?:[0-9a-fA-F]{2})?$|^#(?:[0-9a-fA-F]{4}){1,2}$/', $value) === 1;
}

/**
Expand Down
18 changes: 17 additions & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1928,14 +1928,30 @@ public function testValidateInArray()
public function testValidateHexColor()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['color'=> '#FFF'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#FFFF'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#FFFFFF'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#FFF'], ['color'=>'hex_color']);
$v = new Validator($trans, ['color'=> '#FF000080'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#FF000080'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#00FF0080'], ['color'=>'hex_color']);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['color'=> '#GGG'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['color'=> '#GGGG'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['color'=> '#GGGGGG'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['color'=> '#GGGGGGG'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['color'=> '#FFGG00FF'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['color'=> '#00FF008X'], ['color'=>'hex_color']);
$this->assertFalse($v->passes());
}

public function testValidateConfirmed()
Expand Down

0 comments on commit 6a68969

Please sign in to comment.