Skip to content

Commit

Permalink
Allowing empty id on create.
Browse files Browse the repository at this point in the history
  • Loading branch information
renan committed Apr 5, 2014
1 parent a17c310 commit fadbc20
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Console/Command/Task/ModelTask.php
Expand Up @@ -448,7 +448,9 @@ public function fieldValidation($fieldName, $metaData, $primaryKey) {
}

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

Expand Down
4 changes: 3 additions & 1 deletion src/Console/Templates/default/classes/table.ctp
Expand Up @@ -75,7 +75,9 @@ $key = array_map(function($el) { return "'$el'"; }, (array)$primaryKey);
<?php if ($rule['rule']): ?>
->add('<?= $field ?>', 'valid', ['rule' => '<?= $rule['rule'] ?>'])
<?php endif; ?>
<?php if ($rule['allowEmpty']): ?>
<?php if (is_string($rule['allowEmpty'])): ?>
->allowEmpty('<?= $field ?>', '<?= $rule['allowEmpty'] ?>')
<?php elseif ($rule['allowEmpty']): ?>
->allowEmpty('<?= $field ?>')
<?php else: ?>
->validatePresence('<?= $field ?>', 'create')
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -425,7 +425,7 @@ public function testGetValidation() {
$model = TableRegistry::get('BakeArticles');
$result = $this->Task->getValidation($model);
$expected = [
'id' => ['rule' => 'numeric', 'allowEmpty' => false],
'id' => ['rule' => 'numeric', 'allowEmpty' => 'create'],
'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
'title' => ['rule' => false, 'allowEmpty' => false],
'body' => ['rule' => false, 'allowEmpty' => true],
Expand Down

0 comments on commit fadbc20

Please sign in to comment.