Browse Source

Allowing empty id on create.

Renan Gonçalves 12 years ago
parent
commit
fadbc20a41

+ 3 - 1
src/Console/Command/Task/ModelTask.php

@@ -448,7 +448,9 @@ class ModelTask extends BakeTask {
 		}
 
 		$allowEmpty = false;
-		if ($metaData['null'] === true) {
+		if (in_array($fieldName, $primaryKey)) {
+			$allowEmpty = 'create';
+		} elseif ($metaData['null'] === true) {
 			$allowEmpty = true;
 		}
 

+ 3 - 1
src/Console/Templates/default/classes/table.ctp

@@ -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')

+ 1 - 1
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -425,7 +425,7 @@ class ModelTaskTest extends TestCase {
 		$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],