From 8c5d03c2e4064d94916933ba88526e5c6839b0ee Mon Sep 17 00:00:00 2001 From: Jarkko <1830220+jarkko-hautakorpi@users.noreply.github.com> Date: Thu, 22 Dec 2022 06:50:34 +0200 Subject: [PATCH] feat: always add max: validation to string type fields. (#1046) * Always add max: validation to string type fields. --- src/Common/GeneratorField.php | 2 ++ src/Generators/ModelGenerator.php | 10 +++------- src/Utils/TableFieldsGenerator.php | 6 ++++++ views/model/model.blade.php | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Common/GeneratorField.php b/src/Common/GeneratorField.php index d98391e35..dde87c74e 100644 --- a/src/Common/GeneratorField.php +++ b/src/Common/GeneratorField.php @@ -29,6 +29,8 @@ class GeneratorField public string $foreignKeyText = ''; public int $numberDecimalPoints = 2; + /** @var \Doctrine\DBAL\Schema\Column */ + public $fieldDetails = null; public function parseDBType(string $dbInput) { diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 9b89a4c62..14c97c9b2 100755 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -199,18 +199,14 @@ protected function generateRules(): array $rule[] = 'numeric'; break; case 'string': + case 'text': $rule[] = 'string'; // Enforce a maximum string length if possible. - foreach (explode(':', $field->dbType) as $key => $value) { - if (preg_match('/string,(\d+)/', $value, $matches)) { - $rule[] = 'max:'.$matches[1]; - } + if ((int) $field->fieldDetails->getLength() > 0) { + $rule[] = 'max:'.$field->fieldDetails->getLength(); } break; - case 'text': - $rule[] = 'string'; - break; } $field->validations = implode('|', $rule); diff --git a/src/Utils/TableFieldsGenerator.php b/src/Utils/TableFieldsGenerator.php index 1512defb3..ad5c013d5 100755 --- a/src/Utils/TableFieldsGenerator.php +++ b/src/Utils/TableFieldsGenerator.php @@ -56,6 +56,9 @@ class TableFieldsGenerator /** @var array */ public $ignoredFields; + /** @var \Doctrine\DBAL\Schema\Table */ + public $tableDetails; + public function __construct($tableName, $ignoredFields, $connection = '') { $this->tableName = $tableName; @@ -74,6 +77,8 @@ public function __construct($tableName, $ignoredFields, $connection = '') 'bit' => 'boolean', ]; + $this->tableDetails = $this->schemaManager->listTableDetails($this->tableName); + $mappings = config('laravel_generator.from_table.doctrine_mappings', []); $mappings = array_merge($mappings, $defaultMappings); foreach ($mappings as $dbType => $doctrineType) { @@ -254,6 +259,7 @@ private function generateField($column, $dbType, $htmlType) { $field = new GeneratorField(); $field->name = $column->getName(); + $field->fieldDetails = $this->tableDetails->getColumn($field->name); $field->parseDBType($dbType); //, $column); TODO: handle column param $field->parseHtmlInput($htmlType); diff --git a/views/model/model.blade.php b/views/model/model.blade.php index b500f2218..dab7fef64 100755 --- a/views/model/model.blade.php +++ b/views/model/model.blade.php @@ -29,7 +29,7 @@ class {{ $config->modelNames->name }} extends Model {!! $casts !!} ]; - public static $rules = [ + public static array $rules = [ {!! $rules !!} ];