This commit is contained in:
2021-07-07 18:36:20 +08:00
parent 565943b76f
commit 7eccee5d16
5 changed files with 58 additions and 20 deletions
+15 -11
View File
@@ -22,12 +22,12 @@ class GiiModel extends GiiBase
public ?array $fields;
/**
* ModelFile constructor.
* @param $classFileName
* @param $tableName
* @param $visible
* @param $res
* @param $fields
* GiiModel constructor.
* @param string $classFileName
* @param string $tableName
* @param array $visible
* @param array $res
* @param array $fields
*/
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
{
@@ -308,16 +308,20 @@ use Database\ActiveRecord;
if (empty($data)) return '';
$string = [];
foreach ($data as $key => $_val) {
if (is_string($key) && str_contains($key, ',')) {
$key = '[' . $key . ']';
if (in_array($_val[0][1], $this->type['float'])) {
$e_x = explode(',', $key);
$key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1);
} else if (is_string($key) && str_contains($key, ',')) {
$key = '\'between\' => [' . $key . ']';
} else {
$key = '\'maxLength\' => ' . $key;
}
if (count($_val) == 1) {
[$typeRule, $type, $rule, $field] = current($_val);
$_tmp = '
[\'' . $field . '\', \'' . ($type == 'enum' ? 'enum' : 'maxLength') . '\' => ' . $key . ']';
[\'' . $_val[3] . '\', ' . ($_val[1] == 'enum' ? '\'enum\' => ' . $key : $key) . ']';
} else {
$_tmp = '
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], \'maxLength\' => ' . $key . ']';
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . $key . ']';
}
$string[] = $_tmp;
}
+1 -1
View File
@@ -46,7 +46,7 @@ abstract class BaseValidator
* BaseValidator constructor.
* @param array $config
*/
public function __construct($config = [])
public function __construct(array $config = [])
{
$this->regConfig($config);
}
+5 -8
View File
@@ -37,14 +37,11 @@ class LengthValidator extends BaseValidator
if (is_null($value)) {
return $this->addError('The param :attribute is null');
}
switch (strtolower($this->method)) {
case self::MAX_LENGTH:
return $this->maxLength($value);
case self::MIN_LENGTH:
return $this->minLength($value);
default:
return $this->defaultLength($value);
}
return match (strtolower($this->method)) {
self::MAX_LENGTH => $this->maxLength($value),
self::MIN_LENGTH => $this->minLength($value),
default => $this->defaultLength($value),
};
}
/**
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace validator;
use Exception;
/**
* Class RoundValidator
* @package validator
*/
class RoundValidator extends BaseValidator
{
public ?int $value = null;
/**
* @return bool
* @throws Exception
*/
public function trigger(): bool
{
$value = $this->model->getAttribute($this->field);
if ($value == null || round($value, $this->value) != $value) {
return $this->addError('The param :attribute length error');
}
return true;
}
}
+3
View File
@@ -102,6 +102,9 @@ class Validator extends BaseValidator
'class' => 'validator\LengthValidator',
'method' => 'default',
],
'round' => [
'class' => 'validator\RoundValidator',
],
];
/**