Skip to content

Commit

Permalink
Merge pull request #335 from dmromanov/feature/text-maxlength-validator
Browse files Browse the repository at this point in the history
Add a maxLength rule for text columns
  • Loading branch information
markstory authored Nov 24, 2017
2 parents 076639a + f60967d commit 09de285
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 44 deletions.
48 changes: 27 additions & 21 deletions src/Shell/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,46 +701,52 @@ public function fieldValidation($schema, $fieldName, array $metaData, array $pri
return [];
}

$rule = false;
$rules = [];
if ($fieldName === 'email') {
$rule = 'email';
$rules['email'] = [];
} elseif ($metaData['type'] === 'uuid') {
$rule = 'uuid';
$rules['uuid'] = [];
} elseif ($metaData['type'] === 'integer') {
$rule = 'integer';
$rules['integer'] = [];
} elseif ($metaData['type'] === 'float') {
$rule = 'numeric';
$rules['numeric'] = [];
} elseif ($metaData['type'] === 'decimal') {
$rule = 'decimal';
$rules['decimal'] = [];
} elseif ($metaData['type'] === 'boolean') {
$rule = 'boolean';
$rules['boolean'] = [];
} elseif ($metaData['type'] === 'date') {
$rule = 'date';
$rules['date'] = [];
} elseif ($metaData['type'] === 'time') {
$rule = 'time';
$rules['time'] = [];
} elseif ($metaData['type'] === 'datetime') {
$rule = 'dateTime';
$rules['dateTime'] = [];
} elseif ($metaData['type'] === 'timestamp') {
$rule = 'dateTime';
$rules['dateTime'] = [];
} elseif ($metaData['type'] === 'inet') {
$rule = 'ip';
$rules['ip'] = [];
} elseif ($metaData['type'] === 'string' || $metaData['type'] === 'text') {
$rule = 'scalar';
$rules['scalar'] = [];
if ($metaData['length'] > 0) {
$rules['maxLength'] = [$metaData['length']];
}
}

$allowEmpty = false;
if (in_array($fieldName, $primaryKey)) {
$allowEmpty = 'create';
$rules['allowEmpty'] = ['create'];
} elseif ($metaData['null'] === true) {
$allowEmpty = true;
$rules['allowEmpty'] = [];
} else {
$rules['requirePresence'] = ['create'];
$rules['notEmpty'] = [];
}

$validation = [
'valid' => [
$validation = [];
foreach ($rules as $rule => $args) {
$validation[$rule] = [
'rule' => $rule,
'allowEmpty' => $allowEmpty,
]
];
'args' => $args
];
}

foreach ($schema->constraints() as $constraint) {
$constraint = $schema->getConstraint($constraint);
Expand Down
31 changes: 30 additions & 1 deletion src/View/Helper/BakeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,19 @@ public function getValidationMethods($field, $rules)
$validationMethods = [];

foreach ($rules as $ruleName => $rule) {
if ($rule['rule'] && !isset($rule['provider'])) {
if ($rule['rule'] && !isset($rule['provider']) && !isset($rule['args'])) {
$validationMethods[] = sprintf("->%s('%s')", $rule['rule'], $field);
} elseif ($rule['rule'] && !isset($rule['provider'])) {
$formatTemplate = "->%s('%s')";
if (!empty($rule['args'])) {
$formatTemplate = "->%s('%s', %s)";
}
$validationMethods[] = sprintf(
$formatTemplate,
$rule['rule'],
$field,
implode(', ', $this->escapeArguments($rule['args']))
);
} elseif ($rule['rule'] && isset($rule['provider'])) {
$validationMethods[] = sprintf(
"->add('%s', '%s', ['rule' => '%s', 'provider' => '%s'])",
Expand Down Expand Up @@ -359,6 +370,24 @@ public function getFieldAccessibility($fields = null, $primaryKey = null)
return $accessible;
}

/**
* Wrap string arguments with quotes
*
* @param array $args array of arguments
* @return array
*/
public function escapeArguments($args)
{
return array_map(function ($v) {
if (is_string($v)) {
$v = strtr($v, ["'" => "\'"]);
$v = "'$v'";
}

return $v;
}, $args);
}

/**
* To be mocked elsewhere...
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/BakeArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BakeArticlesFixture extends TestFixture
public $fields = [
'id' => ['type' => 'integer'],
'bake_user_id' => ['type' => 'integer', 'null' => false],
'title' => ['type' => 'string', 'null' => false],
'title' => ['type' => 'string', 'length' => 50, 'null' => false],
'body' => 'text',
'published' => ['type' => 'boolean', 'length' => 1, 'default' => false],
'created' => 'datetime',
Expand Down
138 changes: 138 additions & 0 deletions tests/Fixture/NumberTreesFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* NumberTreeFixture
*
* Generates a tree of data for use testing the tree behavior
*/
class NumberTreesFixture extends TestFixture
{

/**
* fields property
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'length' => 50, 'null' => false],
'parent_id' => 'integer',
'lft' => ['type' => 'integer'],
'rght' => ['type' => 'integer'],
'depth' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
];

/**
* Records
*
* - electronics:1
* - televisions:2
* - tube:3
* - lcd:4
* - plasma:5
* - portable:6
* - mp3:7
* - flash:8
* - cd:9
* - radios:10
* - alien ware: 11
*
* @var array
*/
public $records = [
[
'name' => 'electronics',
'parent_id' => null,
'lft' => '1',
'rght' => '20',
'depth' => 0
],
[
'name' => 'televisions',
'parent_id' => '1',
'lft' => '2',
'rght' => '9',
'depth' => 1
],
[
'name' => 'tube',
'parent_id' => '2',
'lft' => '3',
'rght' => '4',
'depth' => 2
],
[
'name' => 'lcd',
'parent_id' => '2',
'lft' => '5',
'rght' => '6',
'depth' => 2
],
[
'name' => 'plasma',
'parent_id' => '2',
'lft' => '7',
'rght' => '8',
'depth' => 2
],
[
'name' => 'portable',
'parent_id' => '1',
'lft' => '10',
'rght' => '19',
'depth' => 1
],
[
'name' => 'mp3',
'parent_id' => '6',
'lft' => '11',
'rght' => '14',
'depth' => 2
],
[
'name' => 'flash',
'parent_id' => '7',
'lft' => '12',
'rght' => '13',
'depth' => 3
],
[
'name' => 'cd',
'parent_id' => '6',
'lft' => '15',
'rght' => '16',
'depth' => 2
],
[
'name' => 'radios',
'parent_id' => '6',
'lft' => '17',
'rght' => '18',
'depth' => 2
],
[
'name' => 'alien hardware',
'parent_id' => null,
'lft' => '21',
'rght' => '22',
'depth' => 0
]
];
}
Loading

0 comments on commit 09de285

Please sign in to comment.