Today I wanted to show you what I’m planning for Crtx_Form and to get some feedback on my ideas.
Crtx_Form will have three modes of usage:
- OO Interface to build forms directly from PHP (a la HTML_QuickForm
- XHTML-like XML syntax for use in Crtx_Template
- Binding to a Crtx_DB DataObject
What this means is the following:
1. Using PHP OO Interface
- $form = new Crtx_Form;
- $username_element = $form->addElement(‘text‘, ‘username‘, ‘Username‘);
- $password_element = $form->addElement(‘password‘, ‘password‘, ‘Password‘);
- $email_element = $form->addElement(‘text‘, ‘email‘, ‘E-Mail‘);
- // Two ways to add validation
- $form->addValidation(‘username‘, ‘minlen‘, 6); // Use the element name
- $form->addValidation($username_element, ‘type‘, ‘alphanumeric‘); // Use the element object itself
- $form->addValidation(‘password‘, ‘minlen‘, ‘8‘);
- $form->addValidation($password_element, ‘type‘, ‘alphanumeric‘);
- $form->addValidation(‘email‘, ‘regex‘, ‘/^.*@.*$/‘);
- $form->toHTML();
I haven’t decided whether or not to use different objects for each type of element, or even one for all of them – seems like bloat. However,
I’m going to be using DOM to build the form, so I may just use those objects :)
2. Using Crtx_Template
- <crtx:form>
- <crtx:label for=“username“>Usernamecrtx:label>
- <crtx:input type=“text” name=“username” minlength=“6” type=“alphanumeric” />
- <br />
- <crtx:label for=“password“>Passwordcrtx:label>
- <crtx:input type=“password” name=“password” minlength=“8” type=“alphanumeric” />
- <br />
- <crtx:label for=“email“>E-Mailcrtx:label>
- <crtx:input type=“text” name=“e-mail” regex=“/^.*@.*/” />
- crtx:form>
3. Binding to a DataObject
- $form = new Crtx_Form;
- $form->bind(‘My_DataObject_Class‘);
- /* Even when using a DataObject, you still need to add the submit button
- by hand, so its custom :) */
- $form->addElement(‘submit‘, ‘submit‘, ‘Submit Form‘);
OR
- <crtx:form bind=“My_DataObject_Class“>
- <crtx:input type=“submit” name=“submit” value=“Submit Form” />
- crtx:form>
Note, that I didn’t include the submit buttons in the first 2 examples just to be lazy. But they should be there, they are not generated automatically.
Additionally, it will provide both serverside and clientside validation.
Finally, I wanted to say that with the addition of Crtx_Form, whilst it won’t be mandatory (I personally don’t like it much) it will be possible to follow the MVC pattern, using (M) Crtx_DB, (V) Crtx_Template and (C) Crtx_Form.
– Davey