Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add fieldNames() to Form contexts.

Generating inputs based on only an entity requires knowing the field
names, so we'll need a way to reflect that data as well.
  • Loading branch information...
commit 8451e716b62be21ee61ebb6a4d9a70676343e39f 1 parent 4c93e7d
@markstory markstory authored
View
9 src/View/Form/ArrayContext.php
@@ -175,6 +175,15 @@ public function isRequired($field) {
}
/**
+ * {@inheritDoc}
+ */
+ public function fieldNames() {
+ $schema = $this->_context['schema'];
+ unset($schema['_constraints'], $schema['_indexes']);
+ return array_keys($schema);
+ }
+
+/**
* Get the abstract field type for a given field name.
*
* @param string $field A dot separated path to get a schema type for.
View
7 src/View/Form/ContextInterface.php
@@ -62,6 +62,13 @@ public function val($field);
public function isRequired($field);
/**
+ * Get the fieldnames of the top level object in this context.
+ *
+ * @return array A list of the field names in the context.
+ */
+ public function fieldNames();
+
+/**
* Get the abstract field type for a given field name.
*
* @param string $field A dot separated path to get a schema type for.
View
12 src/View/Form/EntityContext.php
@@ -318,6 +318,18 @@ public function isRequired($field) {
}
/**
+ * Get the field names from the top level entity.
+ *
+ * If the context is for an array of entities, the 0th index will be used.
+ *
+ * @return array Array of fieldnames in the table/entity.
+ */
+ public function fieldNames() {
+ $table = $this->_getTable('0');
+ return $table->schema()->columns();
+ }
+
+/**
* Get the validator associated to an entity based on naming
* conventions.
*
View
7 src/View/Form/NullContext.php
@@ -80,6 +80,13 @@ public function isRequired($field) {
/**
* {@inheritDoc}
*/
+ public function fieldNames() {
+ return [];
+ }
+
+/**
+ * {@inheritDoc}
+ */
public function type($field) {
return null;
}
View
14 tests/TestCase/View/Form/EntityContextTest.php
@@ -819,4 +819,18 @@ protected function _setupTables() {
$comments->validator('custom', $validator);
}
+/**
+ * Test the fieldnames method.
+ *
+ * @return void
+ */
+ public function testFieldNames() {
+ $context = new EntityContext($this->request, [
+ 'entity' => new Entity(),
+ 'table' => 'Articles',
+ ]);
+ $articles = TableRegistry::get('Articles');
+ $this->assertEquals($articles->schema()->columns(), $context->fieldNames());
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.