Add field rule setting via array

This commit is contained in:
Visman 2020-10-07 20:56:27 +07:00
parent e8157cc802
commit aa7b179e38
2 changed files with 8 additions and 5 deletions

View file

@ -149,17 +149,20 @@ class Validator
public function addRules(array $list): Validator
{
foreach ($list as $field => $raw) {
$rules = [];
$suffix = null;
// правило для элементов массива
if (\strpos($field, '.') > 0) {
list($field, $suffix) = \explode('.', $field, 2);
}
$rules = [];
$raw = \str_replace('\|', "\0", $raw);
if (! \is_array($raw)) {
$raw = \explode('|', $raw);
}
// перебор правил для текущего поля
foreach (\explode('|', $raw) as $rule) {
$rule = \str_replace("\0", '|', $rule);
foreach ($raw as $rule) {
$vs = \explode(':', $rule, 2);
if (empty($this->validators[$vs[0]])) {

View file

@ -298,7 +298,7 @@ class BBCode extends Parser
'other_attrs.*.format' => 'string:trim|max:1024',
'other_attrs.*.body_format' => 'string:trim|max:1024',
'other_attrs.*.text_only' => 'required|integer|in:0,1',
'new_attr.name' => 'string:trim|regex:%^(?:\|[a-z-]{2,15})$%',
'new_attr.name' => ['string:trim', 'regex:%^(?:|[a-z-]{2,15})$%'],
'new_attr.allowed' => 'required|integer|in:0,1',
'new_attr.required' => 'required|integer|in:0,1',
'new_attr.format' => 'string:trim|max:1024',