Browse Source

Add fieldType to Form\Schema.

This new method will make implementing a form helper context easier.
Mark Story 11 years ago
parent
commit
ff289fbc1c
2 changed files with 34 additions and 0 deletions
  1. 15 0
      src/Form/Schema.php
  2. 19 0
      tests/TestCase/Form/SchemaTest.php

+ 15 - 0
src/Form/Schema.php

@@ -116,4 +116,19 @@ class Schema {
 		return (bool) $field['required'];
 		return (bool) $field['required'];
 	}
 	}
 
 
+/**
+ * Get the type of the named field.
+ *
+ * @param string $field The name of the field.
+ * @return string|null Either the field type or null if the
+ *   field does not exist.
+ */
+	public function fieldType($name) {
+		$field = $this->field($name);
+		if (!$field) {
+			return null;
+		}
+		return $field['type'];
+	}
+
 }
 }

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

@@ -111,4 +111,23 @@ class SchemaTest extends TestCase {
 		$this->assertFalse($schema->isRequired('nope'));
 		$this->assertFalse($schema->isRequired('nope'));
 	}
 	}
 
 
+/**
+ * test fieldType
+ *
+ * @return void
+ */
+	public function testFieldType() {
+		$schema = new Schema();
+
+		$schema->addField('name', 'string')
+			->addField('numbery', [
+				'type' => 'decimal',
+				'required' => true
+			]);
+		$this->assertEquals('string', $schema->fieldType('name'));
+		$this->assertEquals('decimal', $schema->fieldType('numbery'));
+		$this->assertNull($schema->fieldType('nope'));
+	}
+
+
 }
 }