Recent

Author Topic: reading json file and create form at runtime  (Read 216 times)

Packs

  • Sr. Member
  • ****
  • Posts: 368
reading json file and create form at runtime
« on: October 03, 2024, 08:34:40 am »
I want to read json file and create a form object at runtime .

anyone tried this type of coding .

with validation on submit form too
Code: Javascript  [Select][+][-]
  1. [
  2.     {
  3.       sectionTitle: 'User Information',
  4.       fields: [
  5.         {
  6.           name: 'fullName',
  7.           label: 'Full Name',
  8.           type: 'text',
  9.           required: true,
  10.           placeholder: 'Enter full name',
  11.         },
  12.         {
  13.           name: 'email',
  14.           label: 'Email Address',
  15.           type: 'email',
  16.           required: true,
  17.           placeholder: 'Enter email',
  18.         },
  19.         {
  20.           name: 'password',
  21.           label: 'Password',
  22.           type: 'password',
  23.           required: true,
  24.           placeholder: 'Enter password',
  25.         },
  26.         {
  27.           name: 'birthday',
  28.           label: 'Date of Birth',
  29.           type: 'date',
  30.           required: false,
  31.         },
  32.       ],
  33.     },
  34.     {
  35.       sectionTitle: 'Preferences',
  36.       fields: [
  37.         {
  38.           name: 'colorPreference',
  39.           label: 'Favorite Color',
  40.           type: 'color',
  41.           required: false,
  42.         },
  43.         {
  44.           name: 'experienceLevel',
  45.           label: 'Experience Level',
  46.           type: 'range',
  47.           min: 0,
  48.           max: 10,
  49.           step: 1,
  50.           required: false,
  51.         },
  52.         {
  53.           name: 'developmentSkills',
  54.           label: 'Development Skills',
  55.           type: 'select',
  56.           options: [
  57.             { label: 'Frontend', value: 'frontend' },
  58.             { label: 'Backend', value: 'backend' },
  59.             { label: 'Full Stack', value: 'fullstack' },
  60.             { label: 'Data Science', value: 'datascience' },
  61.           ],
  62.           required: true,
  63.         },
  64.       ],
  65.     },
  66.     {
  67.       sectionTitle: 'Feedback',
  68.       fields: [
  69.         {
  70.           name: 'websiteFeedback',
  71.           label: 'Your Feedback',
  72.           type: 'textarea',
  73.           required: false,
  74.           placeholder: 'Share your thoughts',
  75.         },
  76.         {
  77.           name: 'termsAgreement',
  78.           label: 'Agree to Terms',
  79.           type: 'checkbox',
  80.           required: true,
  81.         },
  82.         {
  83.           name: 'fileUpload',
  84.           label: 'Upload File',
  85.           type: 'file',
  86.           required: false,
  87.         },
  88.         {
  89.           name: 'searchQuery',
  90.           label: 'Search',
  91.           type: 'search',
  92.           required: false,
  93.           placeholder: 'Search...',
  94.         },
  95.       ],
  96.     },
  97.   ];
  98.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 8025
Re: reading json file and create form at runtime
« Reply #1 on: October 03, 2024, 09:04:40 am »
My understanding from the devs is that creating a form involves Deep Magic which is beyond the reach of application programming.

I don't know whether there is any other kind of container into which the description could be parsed and then built by invoking e.g. Lazbuild, but I think it would put you at the "bleeding edge".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11938
  • FPC developer.
Re: reading json file and create form at runtime
« Reply #2 on: October 03, 2024, 09:40:20 am »
There is deep magic, but it is done, and that deep magic brought it within the realm of application programming.  Just very complicated, but it can be done.  People load Delphi forms from databases.

Basically you need to parse the json, create classes as you go, and filling properties.

Then you can lookup event handlers for them. I even seen people read script language code for the handler this way, and storing a pointer to the script language code in a .tag, and assigning a generic event handler that does some marshalling and then calls the script language code looked up from the tag.

But it is all a lot of work, and you probably need to build a framework below it. And you can probably manually create hundreds of forms with the same effort.

Lazarus, the LCL and the RTL is in source, you can sources. Particularly pay attention to  the classes unit https://www.freepascal.org/docs-html/rtl/classes/index-5.html  and then specially the read/writecomponent* and object*to* functions

Some searching for components (delphi too!) that do something like this might help too.

(*) modification notice: first edition said python, but it actually was Pascal script. Replace by more general "script"  moniker.
« Last Edit: October 03, 2024, 11:25:56 am by marcov »

 

TinyPortal © 2005-2018