Skip to content

Commit

Permalink
new color validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jturbide committed Aug 3, 2023
1 parent 3aa888b commit e719dcd
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Validation/Validator/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Zemit\Validation\Validator;

use Phalcon\Validation;
use Phalcon\Validation\AbstractValidator;
use Phalcon\Validation\ValidatorInterface;

class Color extends AbstractValidator implements ValidatorInterface
{
protected $template = 'Field :field must be a valid color in hexadecimal format (e.g., #RRGGBB)';

/**
* Constructor
*
* @param array options = [
* 'message' => '',
* 'template' => '',
* 'allowEmpty' => false
* ]
*/
public function __construct(array $options = [])
{
parent::__construct($options);
}

public function validate(Validation $validation, $field): bool
{
$value = $validation->getValue($field);

if (!$this->isValidColor($value)) {

$validation->appendMessage(
$this->messageFactory($validation, $field)
);

return false;
}

return true;
}

/**
* Check if a given color is in a valid hexadecimal format.
*/
private function isValidColor(?string $color): bool
{
// Hexadecimal color regex pattern
$pattern = '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/';

return preg_match($pattern, $color) === 1;
}
}

0 comments on commit e719dcd

Please sign in to comment.