Browse Source

Add method for adding multiple fields at once.

mark_story 11 years ago
parent
commit
a108fe232b
2 changed files with 29 additions and 0 deletions
  1. 13 0
      src/Form/Schema.php
  2. 16 0
      tests/TestCase/Form/SchemaTest.php

+ 13 - 0
src/Form/Schema.php

@@ -38,6 +38,19 @@ class Schema {
 	];
 
 /**
+ * Add multiple fields to the schema.
+ *
+ * @param array $fields The fields to add.
+ * @return $this
+ */
+	public function addFields(array $fields) {
+		foreach ($fields as $name => $attrs) {
+			$this->addField($name, $attrs);
+		}
+		return $this;
+	}
+
+/**
  * Adds a field to the schema.
  *
  * @param string $name The field name.

+ 16 - 0
tests/TestCase/Form/SchemaTest.php

@@ -23,6 +23,22 @@ use Cake\TestSuite\TestCase;
 class SchemaTest extends TestCase {
 
 /**
+ * Test adding multiple fields.
+ *
+ * @return void
+ */
+	public function testAddingMultipleFields() {
+		$schema = new Schema();
+		$schema->addFields([
+			'email' => 'string',
+			'body' => ['type' => 'string', 'length' => 1000]
+		]);
+		$this->assertEquals(['email', 'body'], $schema->fields());
+		$this->assertEquals('string', $schema->field('email')['type']);
+		$this->assertEquals('string', $schema->field('body')['type']);
+	}
+
+/**
  * test adding fields.
  *
  * @return void