Crtx_Form and MVC with Cerebral Cortex

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:

  1. OO Interface to build forms directly from PHP (a la HTML_QuickForm
  2. XHTML-like XML syntax for use in Crtx_Template
  3. Binding to a Crtx_DB DataObject

What this means is the following:

1. Using PHP OO Interface

  2.  $form = new Crtx_Form;
  4.  $username_element = $form->addElement(text, username, Username);
  5.  $password_element = $form->addElement(password, password, Password);
  6.  $email_element = $form->addElement(text, email, E-Mail);
  8.  // Two ways to add validation
  9.  $form->addValidation(username, minlen6); // Use the element name
  10.  $form->addValidation($username_element, type, alphanumeric); // Use the element object itself
  12.  $form->addValidation(password, minlen, 8);
  13.  $form->addValidation($password_element, type, alphanumeric);
  15.  $form->addValidation(email, regex, /^.*@.*$/);
  17.  $form->toHTML();
  18.  ?>

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

  1.  <crtx:form>
  2.      <crtx:label for=username>Usernamecrtx:label>
  3.      <crtx:input type=textname=usernameminlength=6type=alphanumeric/>
  4.      <br />
  5.      <crtx:label for=password>Passwordcrtx:label>
  6.      <crtx:input type=passwordname=passwordminlength=8type=alphanumeric/>
  7.      <br />
  8.      <crtx:label for=email>E-Mailcrtx:label>
  9.      <crtx:input type=textname=e-mailregex=/^.*@.*//>
  10.  crtx:form>

3. Binding to a DataObject

  2.  $form = new Crtx_Form;
  3.  $form->bind(My_DataObject_Class);
  5.  /* Even when using a DataObject, you still need to add the submit button
  6.  by hand, so its custom :) */
  8.  $form->addElement(submit, submit, Submit Form);
  9.  ?>


  1.  <crtx:form bind=My_DataObject_Class>
  2.      <crtx:input type=submitname=submitvalue=Submit Form/>
  3.  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