ryancramerdesign / ProcessWire

Our repository has moved to https://github.com/processwire – please head there for the latest version.
https://processwire.com
Other
727 stars 201 forks source link

YAML Form builder with validation and HTML wrapping (Core) #581

Open klor opened 10 years ago

klor commented 10 years ago

Quoting Ryan with below enhancement request:

Lately I've been thinking that PW should have a YAML to Inputfields conversion tool in the core (something that would be pretty easy to build), so that one could define a form like this:

name: subscribe-form
action: ./
method: post

fields:
  - name: full_name
    label: Your Full Name
    type: Text
    required: 1

  - name: email
    type: Email
    label: Your E-Mail Address
    placeholder: you@company.com
    required: 1

  - name: pass
    type: Password
    label: Your Password
    required: 1

  - name: submit
    type: Submit
    value: Subscribe

And create it like this (where $yaml is the string above above):

$form = $modules->get('InputfieldForm'); $form->load($yaml); echo $form->render();

An extende version would also support:

Resources (hat tip: Soma):

ryancramerdesign commented 10 years ago

Static definition can work with most Inputfields but not all. The options available with each Inputfield depend on what Inputfield it is, because each can define its own options. The availability of those options can also depend on other options or runtime properties. We actually do use static definition with FormBuilder (for storage, export and import), which uses a JSON string. But FormBuilder is also limited in what Inputfields it can use. I've avoided static definition in the core because I don't think it could be universally compatible with all Inputfields. For instance, some Inputfields require runtime injected dependencies as part of their configuration, and that doesn't lend itself well to static definition. Still, static definition can work in a lot of instances (possibly even most), but it's too limited as a core definition method. I can see it being a useful module though, so if you are exploring this I would definitely encourage it and always happy to assist where I can.

Output with support for Bootstrap and other CSS frameworks.

That's possible when dealing with inputs that Bootstrap knows about (like the standard HTML set of inputs), but the possible Inputfields are not bound to the standard set of HTML inputs. Take asmSelect (a type of select[multiple] replacement, for example). So I think it's fine to adopt individual frameworks like Bootstrap for one's own needs, but they are too limited to accommodate all the potential scope of Inputfields in the core. That being said, I do sometimes use Foundation on the front-end for Inputfield forms, but with the understanding that I have to stick to a limited set of Inputfields.

Form validation with patterns (using regex as in HTML5 form attributes, so four digits is equal to "[0-9]{4}").

Form validation with patterns is already built-in, at least for text types. We use the HTML5 pattern attribute, as well as a server side regex.

Form validation based on field type (text, textarea, e-mail, checkbox, url)

Both Fieldtypes and Inputfields already perform their own validation. Fieldtypes do it with their sanitizeValue() method, and Inputfields do it with their processInput() method. Though some Inputfields do it from the setAttribute() method instead, in order to validate both form provided data and API provided data in the same way.

Output that can wrap input fields in TABLE row or DIV HTML tags.

See these methods, and here are the default values.

nicoknoll commented 10 years ago

I don't think it should be in the core. The core includes stuff which is required to run ProcessWire or often used. I think this YAML tool is neither of both.

But of course it is possible to make a simple module for this. (I think somebody already did this recently.)

isellsoap commented 8 years ago

@klor What’s the status of this issue?