Skip to content

Commit 8c5d03c

Browse files
feat: always add max: validation to string type fields. (#1046)
* Always add max: validation to string type fields.
1 parent 2eebc3b commit 8c5d03c

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

src/Common/GeneratorField.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class GeneratorField
2929
public string $foreignKeyText = '';
3030

3131
public int $numberDecimalPoints = 2;
32+
/** @var \Doctrine\DBAL\Schema\Column */
33+
public $fieldDetails = null;
3234

3335
public function parseDBType(string $dbInput)
3436
{

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,14 @@ protected function generateRules(): array
199199
$rule[] = 'numeric';
200200
break;
201201
case 'string':
202+
case 'text':
202203
$rule[] = 'string';
203204

204205
// Enforce a maximum string length if possible.
205-
foreach (explode(':', $field->dbType) as $key => $value) {
206-
if (preg_match('/string,(\d+)/', $value, $matches)) {
207-
$rule[] = 'max:'.$matches[1];
208-
}
206+
if ((int) $field->fieldDetails->getLength() > 0) {
207+
$rule[] = 'max:'.$field->fieldDetails->getLength();
209208
}
210209
break;
211-
case 'text':
212-
$rule[] = 'string';
213-
break;
214210
}
215211

216212
$field->validations = implode('|', $rule);

src/Utils/TableFieldsGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class TableFieldsGenerator
5656
/** @var array */
5757
public $ignoredFields;
5858

59+
/** @var \Doctrine\DBAL\Schema\Table */
60+
public $tableDetails;
61+
5962
public function __construct($tableName, $ignoredFields, $connection = '')
6063
{
6164
$this->tableName = $tableName;
@@ -74,6 +77,8 @@ public function __construct($tableName, $ignoredFields, $connection = '')
7477
'bit' => 'boolean',
7578
];
7679

80+
$this->tableDetails = $this->schemaManager->listTableDetails($this->tableName);
81+
7782
$mappings = config('laravel_generator.from_table.doctrine_mappings', []);
7883
$mappings = array_merge($mappings, $defaultMappings);
7984
foreach ($mappings as $dbType => $doctrineType) {
@@ -254,6 +259,7 @@ private function generateField($column, $dbType, $htmlType)
254259
{
255260
$field = new GeneratorField();
256261
$field->name = $column->getName();
262+
$field->fieldDetails = $this->tableDetails->getColumn($field->name);
257263
$field->parseDBType($dbType); //, $column); TODO: handle column param
258264
$field->parseHtmlInput($htmlType);
259265

views/model/model.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class {{ $config->modelNames->name }} extends Model
2929
{!! $casts !!}
3030
];
3131

32-
public static $rules = [
32+
public static array $rules = [
3333
{!! $rules !!}
3434
];
3535

0 commit comments

Comments
 (0)