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; public ?array $fields;
/** /**
* ModelFile constructor. * GiiModel constructor.
* @param $classFileName * @param string $classFileName
* @param $tableName * @param string $tableName
* @param $visible * @param array $visible
* @param $res * @param array $res
* @param $fields * @param array $fields
*/ */
public function __construct(string $classFileName, string $tableName, array $visible, array $res, 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 ''; if (empty($data)) return '';
$string = []; $string = [];
foreach ($data as $key => $_val) { foreach ($data as $key => $_val) {
if (is_string($key) && str_contains($key, ',')) { if (in_array($_val[0][1], $this->type['float'])) {
$key = '[' . $key . ']'; $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) { if (count($_val) == 1) {
[$typeRule, $type, $rule, $field] = current($_val);
$_tmp = ' $_tmp = '
[\'' . $field . '\', \'' . ($type == 'enum' ? 'enum' : 'maxLength') . '\' => ' . $key . ']'; [\'' . $_val[3] . '\', ' . ($_val[1] == 'enum' ? '\'enum\' => ' . $key : $key) . ']';
} else { } else {
$_tmp = ' $_tmp = '
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], \'maxLength\' => ' . $key . ']'; [[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . $key . ']';
} }
$string[] = $_tmp; $string[] = $_tmp;
} }
+1 -1
View File
@@ -46,7 +46,7 @@ abstract class BaseValidator
* BaseValidator constructor. * BaseValidator constructor.
* @param array $config * @param array $config
*/ */
public function __construct($config = []) public function __construct(array $config = [])
{ {
$this->regConfig($config); $this->regConfig($config);
} }
+5 -8
View File
@@ -37,14 +37,11 @@ class LengthValidator extends BaseValidator
if (is_null($value)) { if (is_null($value)) {
return $this->addError('The param :attribute is null'); return $this->addError('The param :attribute is null');
} }
switch (strtolower($this->method)) { return match (strtolower($this->method)) {
case self::MAX_LENGTH: self::MAX_LENGTH => $this->maxLength($value),
return $this->maxLength($value); self::MIN_LENGTH => $this->minLength($value),
case self::MIN_LENGTH: default => $this->defaultLength($value),
return $this->minLength($value); };
default:
return $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', 'class' => 'validator\LengthValidator',
'method' => 'default', 'method' => 'default',
], ],
'round' => [
'class' => 'validator\RoundValidator',
],
]; ];
/** /**