Skip to content

Commit f60967d

Browse files
committed
Added maxLength() validation rule for text columns when baking tables
1 parent 3c3c512 commit f60967d

10 files changed

Lines changed: 317 additions & 44 deletions

src/Shell/Task/ModelTask.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -701,46 +701,52 @@ public function fieldValidation($schema, $fieldName, array $metaData, $primaryKe
701701
return false;
702702
}
703703

704-
$rule = false;
704+
$rules = [];
705705
if ($fieldName === 'email') {
706-
$rule = 'email';
706+
$rules['email'] = [];
707707
} elseif ($metaData['type'] === 'uuid') {
708-
$rule = 'uuid';
708+
$rules['uuid'] = [];
709709
} elseif ($metaData['type'] === 'integer') {
710-
$rule = 'integer';
710+
$rules['integer'] = [];
711711
} elseif ($metaData['type'] === 'float') {
712-
$rule = 'numeric';
712+
$rules['numeric'] = [];
713713
} elseif ($metaData['type'] === 'decimal') {
714-
$rule = 'decimal';
714+
$rules['decimal'] = [];
715715
} elseif ($metaData['type'] === 'boolean') {
716-
$rule = 'boolean';
716+
$rules['boolean'] = [];
717717
} elseif ($metaData['type'] === 'date') {
718-
$rule = 'date';
718+
$rules['date'] = [];
719719
} elseif ($metaData['type'] === 'time') {
720-
$rule = 'time';
720+
$rules['time'] = [];
721721
} elseif ($metaData['type'] === 'datetime') {
722-
$rule = 'dateTime';
722+
$rules['dateTime'] = [];
723723
} elseif ($metaData['type'] === 'timestamp') {
724-
$rule = 'dateTime';
724+
$rules['dateTime'] = [];
725725
} elseif ($metaData['type'] === 'inet') {
726-
$rule = 'ip';
726+
$rules['ip'] = [];
727727
} elseif ($metaData['type'] === 'string' || $metaData['type'] === 'text') {
728-
$rule = 'scalar';
728+
$rules['scalar'] = [];
729+
if ($metaData['length'] > 0) {
730+
$rules['maxLength'] = [$metaData['length']];
731+
}
729732
}
730733

731-
$allowEmpty = false;
732734
if (in_array($fieldName, $primaryKey)) {
733-
$allowEmpty = 'create';
735+
$rules['allowEmpty'] = ['create'];
734736
} elseif ($metaData['null'] === true) {
735-
$allowEmpty = true;
737+
$rules['allowEmpty'] = [];
738+
} else {
739+
$rules['requirePresence'] = ['create'];
740+
$rules['notEmpty'] = [];
736741
}
737742

738-
$validation = [
739-
'valid' => [
743+
$validation = [];
744+
foreach ($rules as $rule => $args) {
745+
$validation[$rule] = [
740746
'rule' => $rule,
741-
'allowEmpty' => $allowEmpty,
742-
]
743-
];
747+
'args' => $args
748+
];
749+
}
744750

745751
foreach ($schema->constraints() as $constraint) {
746752
$constraint = $schema->getConstraint($constraint);

src/View/Helper/BakeHelper.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,19 @@ public function getValidationMethods($field, $rules)
292292
$validationMethods = [];
293293

294294
foreach ($rules as $ruleName => $rule) {
295-
if ($rule['rule'] && !isset($rule['provider'])) {
295+
if ($rule['rule'] && !isset($rule['provider']) && !isset($rule['args'])) {
296296
$validationMethods[] = sprintf("->%s('%s')", $rule['rule'], $field);
297+
} elseif ($rule['rule'] && !isset($rule['provider'])) {
298+
$formatTemplate = "->%s('%s')";
299+
if (!empty($rule['args'])) {
300+
$formatTemplate = "->%s('%s', %s)";
301+
}
302+
$validationMethods[] = sprintf(
303+
$formatTemplate,
304+
$rule['rule'],
305+
$field,
306+
implode(', ', $this->escapeArguments($rule['args']))
307+
);
297308
} elseif ($rule['rule'] && isset($rule['provider'])) {
298309
$validationMethods[] = sprintf(
299310
"->add('%s', '%s', ['rule' => '%s', 'provider' => '%s'])",
@@ -359,6 +370,24 @@ public function getFieldAccessibility($fields = null, $primaryKey = null)
359370
return $accessible;
360371
}
361372

373+
/**
374+
* Wrap string arguments with quotes
375+
*
376+
* @param array $args array of arguments
377+
* @return array
378+
*/
379+
public function escapeArguments($args)
380+
{
381+
return array_map(function ($v) {
382+
if (is_string($v)) {
383+
$v = strtr($v, ["'" => "\'"]);
384+
$v = "'$v'";
385+
}
386+
387+
return $v;
388+
}, $args);
389+
}
390+
362391
/**
363392
* To be mocked elsewhere...
364393
*

tests/Fixture/BakeArticlesFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BakeArticlesFixture extends TestFixture
3030
public $fields = [
3131
'id' => ['type' => 'integer'],
3232
'bake_user_id' => ['type' => 'integer', 'null' => false],
33-
'title' => ['type' => 'string', 'null' => false],
33+
'title' => ['type' => 'string', 'length' => 50, 'null' => false],
3434
'body' => 'text',
3535
'published' => ['type' => 'boolean', 'length' => 1, 'default' => false],
3636
'created' => 'datetime',
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license https://opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Bake\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* NumberTreeFixture
21+
*
22+
* Generates a tree of data for use testing the tree behavior
23+
*/
24+
class NumberTreesFixture extends TestFixture
25+
{
26+
27+
/**
28+
* fields property
29+
*
30+
* @var array
31+
*/
32+
public $fields = [
33+
'id' => ['type' => 'integer'],
34+
'name' => ['type' => 'string', 'length' => 50, 'null' => false],
35+
'parent_id' => 'integer',
36+
'lft' => ['type' => 'integer'],
37+
'rght' => ['type' => 'integer'],
38+
'depth' => ['type' => 'integer'],
39+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
40+
];
41+
42+
/**
43+
* Records
44+
*
45+
* - electronics:1
46+
* - televisions:2
47+
* - tube:3
48+
* - lcd:4
49+
* - plasma:5
50+
* - portable:6
51+
* - mp3:7
52+
* - flash:8
53+
* - cd:9
54+
* - radios:10
55+
* - alien ware: 11
56+
*
57+
* @var array
58+
*/
59+
public $records = [
60+
[
61+
'name' => 'electronics',
62+
'parent_id' => null,
63+
'lft' => '1',
64+
'rght' => '20',
65+
'depth' => 0
66+
],
67+
[
68+
'name' => 'televisions',
69+
'parent_id' => '1',
70+
'lft' => '2',
71+
'rght' => '9',
72+
'depth' => 1
73+
],
74+
[
75+
'name' => 'tube',
76+
'parent_id' => '2',
77+
'lft' => '3',
78+
'rght' => '4',
79+
'depth' => 2
80+
],
81+
[
82+
'name' => 'lcd',
83+
'parent_id' => '2',
84+
'lft' => '5',
85+
'rght' => '6',
86+
'depth' => 2
87+
],
88+
[
89+
'name' => 'plasma',
90+
'parent_id' => '2',
91+
'lft' => '7',
92+
'rght' => '8',
93+
'depth' => 2
94+
],
95+
[
96+
'name' => 'portable',
97+
'parent_id' => '1',
98+
'lft' => '10',
99+
'rght' => '19',
100+
'depth' => 1
101+
],
102+
[
103+
'name' => 'mp3',
104+
'parent_id' => '6',
105+
'lft' => '11',
106+
'rght' => '14',
107+
'depth' => 2
108+
],
109+
[
110+
'name' => 'flash',
111+
'parent_id' => '7',
112+
'lft' => '12',
113+
'rght' => '13',
114+
'depth' => 3
115+
],
116+
[
117+
'name' => 'cd',
118+
'parent_id' => '6',
119+
'lft' => '15',
120+
'rght' => '16',
121+
'depth' => 2
122+
],
123+
[
124+
'name' => 'radios',
125+
'parent_id' => '6',
126+
'lft' => '17',
127+
'rght' => '18',
128+
'depth' => 2
129+
],
130+
[
131+
'name' => 'alien hardware',
132+
'parent_id' => null,
133+
'lft' => '21',
134+
'rght' => '22',
135+
'depth' => 0
136+
]
137+
];
138+
}

0 commit comments

Comments
 (0)