Jump to content

Forms generation from templates


Mastodont

Recommended Posts

Hi all,

 

I want to develop form generator taking input from some kind of template (preferably YAML). Well, I've searched larger frameworks and other sources, but it seems that all people write rather small helpers consisting of methods like AddTextInput, AddDropDown and so on.

 

What I bear in mind? The YAML template would look like this (only draft):

form:
  name: user
  method: post                # post/get, implicit: post, can be omitted
  action: some.php
  enc: multipart/form-data    # implicit: multipart/form-data, can be omitted
layouts:
  table: { hints: under, style: default }
  \# options: html (lines terminated with <br>, control groups made by FIELDSET tag)
  \#          table (implicit 2 cols, left column for labels, right column for controls)
  \# implicit - html: { labels: upon, hints: mouseover, style: default }
controls:
  name:
    type: input
    label: 'Name:'
    hint: 'Type your name here'
    args: { length: 20 }
    validation: [required, 'strip all tags' ]
  surname:
    type: input
    label: 'Surname:'
    hint: 'Type your surname here'
    args: {length: 20}
    validation: [required, 'strip all tags' ]
  save:
    type: submit
    args: { value: 'Save to database' }

 

And main PHP code would be very, very short, as all HTML rendering should be hidden in class:

 

$form = New FormBuilder('user.yaml');
if ($form->Render()) { echo $form->OutputHTML; }

 

Well, it seems as so obvious idea to me, that i can't believe it wasn't yet written. Haven't you seen anything like that?

Link to comment
Share on other sites

One thing you should be aware of with YAML is that there seems to be only one parser, and it's an extension.  Using it will mean limiting the number of people who can use your solution.

 

What you're talking about is indeed neat.  I wonder if it wouldn't make more sense to just use XML (which I suppose you deliberately avoided if you picked YAML) and XSLTs to accomplish the transformation you're talking about. 

 

One reason drawback I see to both approaches is the additional step that is required to insert dynamic content.  For example, if you wanted the form's input fields to have values how would you allow for that?  Going the XML/XSLT route would mean you'd use a post processing step.  If you parsed the scheme (be it YAML or XML) with PHP you could possibly insert the variables along the way.

 

However thinking about it that way it isn't clear -- at least as I write this -- that you're saving time or energy over writing a standard template such as:

<form>
<input name="$foo">

And processing it in PHP as yet another PHP script.  You could then include templates within templates and acheive reuse of visual elements.

 

At any rate this is a very interesting idea that you've proposed, and let's talk some more about the details and trade offs involved.  Be sure to keep us up-to-date with your progress.

 

Buyo

Link to comment
Share on other sites

Thanks for your suggestions.

 

I prefer YAML to XML on account of easy writing such templates. XML without proper editor is clumsy, whereas YAML can be composed anywhere. But I agree that XML + XSLT are another commendable way.

 

Dynamic content – yes, I think of it. The class certainly may split rendering into two phases – preprocessing (in constructor) and final rendering. After preprocessing there would be a place for methods like FillValue($control, $value), OmitField($control) and so on.

 

Standard PHP templates are fine, too .. but again, their composition is more tedious.

Link to comment
Share on other sites

You know I'm not actually ready to jump on the YAML bandwagon just yet.  I have found the longer instances examples YAML are harder to read than xml because without the tag you don't know what the data is necessarily.

 

Anyway, I'm excited by the idea, but don't have any time to play with it on my own.  So come back and tell me what you find.

Link to comment
Share on other sites

come back and tell me what you find

 

A basic sketch is completed, it was simple, but now there are questions concerning rendering of final form.

 

HTML form can be rendered as:

- table

- classical HTML form (fieldset, legend, usually one control per row)

- definition list (queer, I don't want to implement it)

- other way (?)

 

For start I'll develop only first two simple layouts, but what all should I take into account?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.