Skip to content

Commit 69a1460

Browse files
committed
Merge pull request #62 from cakephp/issue-61
Exclude foriegn keys from validation rules.
2 parents bd0dc50 + 60ddc84 commit 69a1460

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Shell/Task/ModelTask.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function bake($name)
112112
$primaryKey = $this->getPrimaryKey($model);
113113
$displayField = $this->getDisplayField($model);
114114
$fields = $this->getFields($model);
115-
$validation = $this->getValidation($model);
115+
$validation = $this->getValidation($model, $associations);
116116
$rulesChecker = $this->getRules($model, $associations);
117117
$behaviors = $this->getBehaviors($model);
118118

@@ -444,9 +444,10 @@ public function getHiddenFields($model)
444444
* Generate default validation rules.
445445
*
446446
* @param \Cake\ORM\Table $model The model to introspect.
447+
* @param array $associations The associations list.
447448
* @return array The validation rules.
448449
*/
449-
public function getValidation($model)
450+
public function getValidation($model, $associations = [])
450451
{
451452
if (!empty($this->params['no-validation'])) {
452453
return [];
@@ -459,8 +460,16 @@ public function getValidation($model)
459460

460461
$validate = [];
461462
$primaryKey = (array)$schema->primaryKey();
462-
463+
$foreignKeys = [];
464+
if (isset($associations['belongsTo'])) {
465+
foreach ($associations['belongsTo'] as $assoc) {
466+
$foreignKeys[] = $assoc['foreignKey'];
467+
}
468+
}
463469
foreach ($fields as $fieldName) {
470+
if (in_array($fieldName, $foreignKeys)) {
471+
continue;
472+
}
464473
$field = $schema->column($fieldName);
465474
$validation = $this->fieldValidation($fieldName, $field, $primaryKey);
466475
if (!empty($validation)) {

tests/TestCase/Shell/Task/ModelTaskTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,29 @@ public function testGetValidation()
660660
$this->assertEquals($expected, $result);
661661
}
662662

663+
/**
664+
* test getting validation rules and exempting foreign keys
665+
*
666+
* @return void
667+
*/
668+
public function testGetValidationExcludeForeignKeys()
669+
{
670+
$model = TableRegistry::get('BakeArticles');
671+
$associations = [
672+
'belongsTo' => [
673+
'BakeUsers' => ['foreignKey' => 'bake_user_id'],
674+
]
675+
];
676+
$result = $this->Task->getValidation($model, $associations);
677+
$expected = [
678+
'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
679+
'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
680+
'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
681+
'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
682+
];
683+
$this->assertEquals($expected, $result);
684+
}
685+
663686
/**
664687
* test getting validation rules with the no-rules param.
665688
*

0 commit comments

Comments
 (0)