diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index 62ed4173..d7d350b7 100644 --- a/app/src/Controller/StandardController.php +++ b/app/src/Controller/StandardController.php @@ -80,6 +80,21 @@ public function add() { $this->render('/Standard/add-edit-view'); } + /** + * Standard operations before the view is rendered. + * + * @since COmanage Match v1.2.0 + * @param EventInterface $event BeforeRender event + * @return \Cake\Http\Response HTTP Response + */ + + public function beforeRender(\Cake\Event\EventInterface $event) { + // Provide some hints to the views + $this->getFieldTypes(); + + return parent::beforeRender($event); + } + /** * Handle a delete action for a Standard object. * @@ -294,6 +309,29 @@ public function generateRedirect() { return $this->redirect($redirect); } + /** + * Make a list of fields types suitable for FieldHelper + * + * @since COmanage Match v1.2.0 + */ + + protected function getFieldTypes() { + // $this->name = Models (ie: from ModelsTable) + $modelsName = $this->name; + // $table = the actual table object + $table = $this->$modelsName; + + $schema = $table->getSchema(); + + // We don't pass the schema object as is, partly because cake might change it + // and partly to simplify access to the parts the views (FieldHelper, really) + // actually need. + + // Note the schema does have field lengths for strings, but typeMap + // doesn't return them and we're not doing anything with them at the moment. + $this->set('vv_field_types', $schema->typeMap()); + } + /** * Generate an index for a set of Standard Objects. * diff --git a/app/src/View/Helper/FieldHelper.php b/app/src/View/Helper/FieldHelper.php index 9855fb0c..a771355a 100644 --- a/app/src/View/Helper/FieldHelper.php +++ b/app/src/View/Helper/FieldHelper.php @@ -69,6 +69,10 @@ public function control(string $fieldName, $label = null; $desc = null; + + // Get the field type from the map of fields (e.g. 'boolean', 'string', 'timestamp') + $fieldMap = $this->getView()->get('vv_field_types'); + $fieldType = $fieldMap[$fieldName]; if($labelText) { $label = $labelText; @@ -115,6 +119,11 @@ public function control(string $fieldName, $control = $this->viewObj->$fieldName->nice(); $isRequired = false; } else { + if($fieldType == 'boolean') { + // A boolean field is a checkbox. Include the label with the field. + $coptions['label'] = $label; + $coptions['class'] = 'form-check-input'; + } $control = $this->Form->control($fieldName, $coptions); } @@ -140,7 +149,7 @@ public function control(string $fieldName, return '