Browse Source

Baking uniqueness rules for email and username fields automatically

Jose Lorenzo Rodriguez 12 years ago
parent
commit
a578cd5745

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

@@ -447,10 +447,21 @@ class ModelTask extends BakeTask {
 			$allowEmpty = true;
 		}
 
-		return [
-			'rule' => $rule,
-			'allowEmpty' => $allowEmpty,
+		$validation = [
+			'valid' => [
+				'rule' => $rule,
+				'allowEmpty' => $allowEmpty,
+			]
 		];
+
+		if (in_array($fieldName, ['email', 'username'])) {
+			$validation['unique'] = [
+				'rule' => 'validateUnique',
+				'provider' => 'table'
+			];
+		}
+
+		return $validation;
 	}
 
 /**

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

@@ -73,10 +73,14 @@ $key = array_map(function($el) { return "'$el'"; }, (array)$primaryKey);
 		$validator
 <?php $countValidation = count($validation) - 1; ?>
 <?php $i = 0; ?>
-<?php foreach ($validation as $field => $rule): ?>
-<?php if ($rule['rule']): ?>
-			->add('<?= $field ?>', 'valid', ['rule' => '<?= $rule['rule'] ?>'])
+<?php foreach ($validation as $field => $rules): ?>
+<?php foreach ($rules as $ruleName => $rule): ?>
+<?php if ($rule['rule'] && !isset($rule['provider'])): ?>
+			->add('<?= $field ?>', '<?= $ruleName ?>', ['rule' => '<?= $rule['rule'] ?>'])
+<?php elseif ($rule['rule'] && isset($rule['provider'])): ?>
+			->add('<?= $field ?>', '<?= $ruleName ?>', ['rule' => '<?= $rule['rule'] ?>', 'provider' => '<?= $rule['provider'] ?>'])
 <?php endif; ?>
+<?php if (isset($rule['allowEmpty'])) : ?>
 <?php if (is_string($rule['allowEmpty'])): ?>
 			->allowEmpty('<?= $field ?>', '<?= $rule['allowEmpty'] ?>')<?= $i === $countValidation ? ";\n" : "\n" ?>
 <?php elseif ($rule['allowEmpty']): ?>
@@ -85,6 +89,8 @@ $key = array_map(function($el) { return "'$el'"; }, (array)$primaryKey);
 			->validatePresence('<?= $field ?>', 'create')
 			->notEmpty('<?= $field ?>')<?= $i === $countValidation ? ";\n" : "\n" ?>
 <?php endif ?>
+<?php endif ?>
+<?php endforeach ?>
 <?php $i++; ?>
 <?php endforeach ?>
 <?php echo "\n"; ?>

+ 38 - 24
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -423,22 +423,22 @@ class ModelTaskTest extends TestCase {
 		$model = TableRegistry::get('BakeArticles');
 		$result = $this->Task->getValidation($model);
 		$expected = [
-			'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
-			'title' => ['rule' => false, 'allowEmpty' => false],
-			'body' => ['rule' => false, 'allowEmpty' => true],
-			'published' => ['rule' => 'boolean', 'allowEmpty' => true],
-			'id' => ['rule' => 'numeric', 'allowEmpty' => 'create']
+			'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
+			'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
+			'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
+			'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
+			'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
 		];
 		$this->assertEquals($expected, $result);
 
 		$model = TableRegistry::get('BakeComments');
 		$result = $this->Task->getValidation($model);
 		$expected = [
-			'bake_article_id' => ['rule' => 'numeric', 'allowEmpty' => false],
-			'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
-			'comment' => ['rule' => false, 'allowEmpty' => true],
-			'published' => ['rule' => false, 'allowEmpty' => true],
-			'otherid' => ['rule' => 'numeric', 'allowEmpty' => 'create']
+			'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
+			'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
+			'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
+			'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
+			'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
 		];
 		$this->assertEquals($expected, $result);
 	}
@@ -546,20 +546,30 @@ class ModelTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeTableValidation() {
-		$validation = array(
-			'id' => array(
-				'allowEmpty' => 'create',
-				'rule' => 'numeric',
-			),
-			'name' => array(
-				'allowEmpty' => false,
-				'rule' => false,
-			),
-			'email' => array(
-				'allowEmpty' => true,
-				'rule' => 'email',
-			),
-		);
+		$validation = [
+			'id' => [
+				'valid' => array(
+					'allowEmpty' => 'create',
+					'rule' => 'numeric',
+				)
+			],
+			'name' => [
+				'valid' => [
+					'allowEmpty' => false,
+					'rule' => false,
+				]
+			],
+			'email' => [
+				'valid' => [
+					'allowEmpty' => true,
+					'rule' => 'email'
+				],
+				'unique' => [
+					'rule' => 'validateUnique',
+					'provider' => 'table'
+				]
+			]
+		];
 		$model = TableRegistry::get('BakeArticles');
 		$result = $this->Task->bakeTable($model, compact('validation'));
 
@@ -570,6 +580,10 @@ class ModelTaskTest extends TestCase {
 		$this->assertContains('public function validationDefault(Validator $validator) {', $result);
 		$this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
 		$this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
+		$this->assertContains(
+			"->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
+			$result)
+		;
 		$this->assertContains("->allowEmpty('id', 'create')", $result);
 		$this->assertContains("->allowEmpty('email')", $result);
 		$this->assertContains("->validatePresence('name', 'create')", $result);