From 4c153911e12ec1cfdf24d3248895641c47ee686b Mon Sep 17 00:00:00 2001 From: Arlen Johnson Date: Thu, 30 Mar 2023 11:57:55 -0400 Subject: [PATCH] Ensure checkbox labels are presented correctly on forms. (CO-2628) --- app/src/Controller/StandardController.php | 38 +++++++++++++++++++++++ app/src/View/Helper/FieldHelper.php | 11 ++++++- app/webroot/css/co-base.css | 15 ++------- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index 62ed41730..d7d350b70 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 9855fb0c7..a771355a8 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 '
  • - ' . ($this->editable + ' . ($this->editable && !($fieldType == 'boolean') ? $this->Form->label($fieldName, $label) : $label) . ($isRequired ? ' *' : '') . ' diff --git a/app/webroot/css/co-base.css b/app/webroot/css/co-base.css index 73664361e..84cbbea0f 100644 --- a/app/webroot/css/co-base.css +++ b/app/webroot/css/co-base.css @@ -1017,7 +1017,7 @@ ul.form-list li { border-bottom: 1px solid var(--cmg-color-lightgray-005); border-right: 1px solid var(--cmg-color-lightgray-005); margin: 0; - padding: 0.5em; + padding: 0.8em 0.5em; list-style: none; } ul.form-list li:nth-child(even) { @@ -1097,19 +1097,8 @@ ul.form-list li.field-stack textarea { width: 100%; resize: vertical; } -.checkbox { - margin-bottom: 0.5em; -} .checkbox input { - float: left; - margin: 4px 4px 0 0; -} -.checkbox label { - margin-left: 1.5em; - display: block; -} -.checkbox label::first-line { - margin-left: 0; + margin: 0.25em 0.5em 0 0; } .checkbox .subfield { margin-left: 1.5em;