[Bps-public-commit] rt-extension-formtools branch dynamic-forms-from-config created. 0.53-3-g780eef8

BPS Git Server git at git.bestpractical.com
Fri Sep 1 20:58:21 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt-extension-formtools".

The branch, dynamic-forms-from-config has been created
        at  780eef8a7df62d6808adeefc796ad61515cf3d9c (commit)

- Log -----------------------------------------------------------------
commit 780eef8a7df62d6808adeefc796ad61515cf3d9c
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Sep 1 16:57:22 2023 -0400

    Create new page to dynamically generate forms from config

diff --git a/html/Forms/dhandler b/html/Forms/dhandler
new file mode 100644
index 0000000..8205ab0
--- /dev/null
+++ b/html/Forms/dhandler
@@ -0,0 +1,127 @@
+<&|/FormTools/Form, next => $form_name . '/' . $form_config->{$page}{'next'} &>
+
+<%perl>
+# Build the current page here dyamically from config
+
+foreach my $element ( @{$form_config->{$page}{'content'}} ) {
+    if ( $element->{type} eq 'raw_html' ) {
+        $m->out( $element->{html} );
+    }
+    elsif ( $element->{type} eq 'component' ) {
+        $m->comp('/FormTools/' . $element->{comp_name}, %{$element->{arguments}});
+    }
+}
+</%perl>
+
+<& /FormTools/Next &>
+</&>
+<%init>
+
+my $path = $m->dhandler_arg;
+my ($form_name, $page_name);
+
+if ( $path =~ /^(\w+)\/(\w+)$/ ) {
+    $form_name = $1;
+    $page_name = $2;
+}
+else {
+    $form_name = $path;
+}
+
+# Limit to names to letters and numbers and underscore
+unless ( $form_name =~ /^\w+$/ ) {
+    Abort('Invalid form name');
+}
+
+if ( $page_name ) {
+    unless ( $page_name =~ /^\w+$/ ) {
+        Abort('Invalid page name');
+    }
+}
+
+# Load FormTools configration and look for a configured
+# form with the provided name.
+
+my %form_config_stub = (
+    'form1' => {
+        'formtools-start-page' => 'page1',
+        'page1' => {
+            name => 'Page One',
+            next => 'page2',
+            content => [
+                {
+                    type => 'raw_html',
+                    html => '<h1>Account Request</h1>',
+                },
+                {
+                    type => 'raw_html',
+                    html => '<h2>Enter your preferred username</h2>',
+                },
+                {
+                    type => 'component',
+                    comp_name => 'Field',
+                    arguments => {
+                        name => 'Username',
+                        default => 'preferred username',
+                    },
+                },
+            ],
+         },
+        'page2' => {
+            name => 'Page Two',
+            next => 'page3',
+            validation => 1,
+            content => [
+                {
+                    type => 'raw_html',
+                    html => '<h1>Account Request</h1>',
+                },
+                {
+                    type => 'component',
+                    comp_name => 'Field',
+                    arguments => {
+                        name => 'Requestors',
+                        label => 'Requested by',
+                        default => $session{'CurrentUser'}->EmailAddress,
+                    },
+                },
+                {
+                    type => 'component',
+                    comp_name => 'Field',
+                    arguments => {
+                        name => 'Systems',
+                    },
+                },
+            ],
+        },
+    },
+    'form2' => {
+        'formtools-start-page' => 'page1',
+        'page1' => { name => 'Page One' },
+        'page2' => { name => 'Page Two' },
+    },
+);
+
+my $form_config = $form_config_stub{$form_name};
+
+unless ( $form_config ) {
+    # We didn't find a form, so show a not found page
+    Abort('Form not found');
+}
+
+my $page;
+if ( $_form_tools_next ) {
+    $page = $_form_tools_next;
+}
+elsif ( $page_name ) {
+    $page = $page_name;
+}
+else {
+    $page = $form_config->{'formtools-start-page'};
+}
+
+
+</%init>
+<%args>
+$_form_tools_next => undef
+</%args>

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt-extension-formtools


More information about the Bps-public-commit mailing list