Blog

Names are powerful; they triangulate an object’s meaning and guide your thoughts. We decided “porkbuns.net” meant we were quirky and Asian. This blog is my chance to show off those qualities.
Jan
19

Savant2+Formation

The Why

Savant2 is my PHP template engine of choice because I don't believe that designers are too dumb to learn a programming language. If you don't trust people you're working with to be able to learn a language, why are you expecting them to learn the syntax of say Smarty (which is even more nonstandard than PHP...)

Savant does not however, have many plugins, and life is just hard if you reinvent the wheel every time you write a form. So I made a form plugin modeled after Formation, but with a Savant plug in handling output. Not content to simply steal an API, I also piggy backed off of the existing Savant form plugin to generate HTML, in the hopes upstream improvements would trickle down.

The How

Forms are constructed as a new class, with a constructor that defines all of the form elements:

PHP:
  1. class SignupForm extends form2_Form {
  2. var $calendar;
  3. var $pin = null;
  4. var $values = array();
  5.  
  6. function SignupForm(&$calendar, $days, $times, $day, $time) {
  7.  
  8. $this->calendar = $calendar;
  9.  
  10. //Name field
  11. $i = &new form2_Text('join_name', 'Name');
  12. // add a validator
  13. // addValidator(function to call, message if failed)
  14. $i->addValidator(array(&$this, 'required'), 'Please enter your name');
  15. $this->addElement($i);
  16.  
  17. // Timeslot field
  18. $h = array(null=>'');
  19. foreach ($days as $j) {
  20. $h[$j] = gmdate('l (jS)', $j);
  21. }
  22. $i = &new form2_Select('join_date', 'Timeslot', $h, $day);
  23. $i->addValidator(array(&$this, 'required'), 'Please select a day');
  24. $this->addElement($i);
  25.  
  26. // Submit button
  27. $i = &new form2_Submit('submit', '', 'Sign Up!');
  28. $this->addElement($i);
  29.  
  30. $this->form2_Form();
  31. }
  32. }

Then, to process the form, you well... err. Override the process method...

PHP:
  1. class SignupForm extends form2_Form {
  2. //... constructor...
  3.  
  4. function process($values) {
  5. // you can access form fields like this...
  6. $join_name = $values['join_name'];
  7.  
  8. // ... put form processing code here
  9. }
  10. }

The Where

So check out the download (zip). Totally unsupported, I'd honestly be surprised if anyone got it working (or cared). But if you run into troubles, contact me, and I'll do what I can to *cough* actually document it.

PS: if you're curious, you can check out the source just to see what it is you're getting into. (Undocumented territory ahoy!)

Filed under: uncategorized