mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-14 08:34:39 +00:00
MVC / CSVListField: add MaskPerItem toggle to allow regex validation per CSV
This commit is contained in:
parent
e08a49332a
commit
4f02754dfc
@ -28,7 +28,7 @@
|
||||
|
||||
namespace OPNsense\Base\FieldTypes;
|
||||
|
||||
use OPNsense\Phalcon\Filter\Validation\Validator\Regex;
|
||||
use OPNsense\Base\Validators\CallbackValidator;
|
||||
|
||||
/**
|
||||
* Class CSVListField
|
||||
@ -64,6 +64,11 @@ class CSVListField extends BaseField
|
||||
*/
|
||||
protected $internalMask = null;
|
||||
|
||||
/**
|
||||
* @var bool marks if regex validation should occur on a per-item basis.
|
||||
*/
|
||||
protected $internalMaskPerItem = false;
|
||||
|
||||
/**
|
||||
* set validation mask
|
||||
* @param string $value regexp validation mask
|
||||
@ -73,6 +78,15 @@ class CSVListField extends BaseField
|
||||
$this->internalMask = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* set mask per csv
|
||||
* @param bool $value
|
||||
*/
|
||||
public function setMaskPerItem($value)
|
||||
{
|
||||
$this->internalMaskPerItem = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve data including selection options (from setSelectOptions)
|
||||
* @return array
|
||||
@ -133,8 +147,27 @@ class CSVListField extends BaseField
|
||||
{
|
||||
$validators = parent::getValidators();
|
||||
if ($this->internalValue != null && $this->internalMask != null) {
|
||||
$validators[] = new Regex(array('message' => $this->internalValidationMessage,
|
||||
'pattern' => trim($this->internalMask)));
|
||||
$validators[] = new CallbackValidator(["callback" => function($data) {
|
||||
$regex_match = function($value, $pattern) {
|
||||
$matches = [];
|
||||
preg_match(trim($pattern), $value, $matches);
|
||||
return $matches[0] == $value;
|
||||
};
|
||||
|
||||
if ($this->internalMaskPerItem) {
|
||||
$items = explode($this->separatorchar, $this->internalValue);
|
||||
foreach ($items as $item) {
|
||||
if (!$regex_match($item, $this->internalMask)) {
|
||||
return ["\"" . $item . "\" is invalid. " . $this->internalValidationMessage];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!$regex_match($this->internalValue, $this->internalMask)) {
|
||||
return [$this->internalValidationMessage];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}]);
|
||||
}
|
||||
return $validators;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user