views:

57

answers:

2

I have recently begun working on a PHP/JS Form Class that will also include a SQL Form builder (eg. building simple forms from sql and auto inserts/updates).

I have tried several classes (zend_form, clonefish, PHP Form Builder Class, phorms etc) but as yet haven't come across a complete solution that is simple, customizable and complete (both server side and client side validation, covers all simple html elements and lots of dhtml elements: sorting, wysiwyg, mutli file upload, date picker, ajax validation etc)

My question is why do some "classes" implement elements via an array and others via proper OO class calls.

eg. Clonefish (popular commercial php class):

    $config = Array( 

  'username' => Array( 
    'type'           => 'inputText', 
    'displayname'    => 'Username', 
    validation     => Array( 
      Array(  
        'type'    => 'string', 
        'minimum' => 5, 
        'maximum' => 15, 
      ), 
    ), 
  ));

$clonefish = new clonefish( 'loginform', 'test.php', 'POST' ); 

$clonefish->addElements( $config, $_POST );

Then others eg. Zend_Form

$form = new Zend_Form;
$username = new Zend_Form_Element_Text('username');
$username->addValidator(new Zend_Validate_Alnum());
$form->addElement($username);

I realise Zend_Form can pass elements in via an array similar to clonefish but why do this?

Is there any benefit? It seems to make things more complicated especially when using a proper IDE like Komodo.

Any thoughts would be appreciated as I dont want to get too far down the track and realize there was great benefit in using arrays to add elements (although this wouldn't be much of a task to add on).

Cheers

+1  A: 

My question is why do some "classes" implement elements via an array and others via proper OO class calls.

For convenience. It's less verbose and it feels less like coding and more like configuration and you need less intimate knowledge of the API.

Btw, the reason you have not yet come across a complete solution that is simple, customizable and complete is because it is not simple. Forms, their validation and rendering is complex, especially if you want to have it customizable for any purpose. ZF's form components are a good example of how to properly decouple and separate all concerns to get the ultimate extensible form builder (including client side code through Zend_Dojo or ZendX_Jquery). But they are also a great example of the complexity required for this. Even with the convenient array configuration, it is damn difficult to make them bend to your will, especially if you need to depart from the default configuration and rendering.

Gordon
You said very diplomatically what I would say as: Zend Framework is a paragon of overengineering and I don't know if emulating them is a worthwhile goal. http://en.wikipedia.org/wiki/Overengineering
beamrider9
Thanks for your response. I agree that it "feels" (well, "looks") like less coding but dont you need just as much knowledge of the API since the "config" options are explicit strings. eg. in the conlonefish example 'type'=>'inputText'. As I've mentioned when using an IDE this makes it infinitely more tedious. I completely agree that HTML4 forms are insanely complex (due to the amount of variation and inconsistent way elements are handled eg. if a multi checkbox isn't selected no variable is sent via POST), and as everyone has mentioned zend is overly complicated. But is that it? Just for "UX"?
Josh Stuart
@Josh Isnt "UX" a rather important point? Even if it's "just" for the developer? You have a point with having to know config strings, but IMO knowing these requires still less knowledge about the classes and their methods than going OO on them. - on a sidenote: I dont think Zend_Form is overly complicated or overengineered *for what it allows*. I am pretty sure someone could come up with a form builder that's easier to use and more suited for a *specific* kind of form but Zend_Form specificially is aimed at *any* form.
Gordon
So it is really a matter of opinion and there is no performance benefit or major pitfall in not implementing via arrays. I think I'll stick to OO initialization with the possible view of adding an array "config" style function in the future.
Josh Stuart
+1  A: 

Why to use objects? Becouase they are a much more complex types. Consider the following example (I never useed Zend_Form so I don't even know its architecture):

class MySuperAlnumValidator extends Zend_Validate_Alnum {
     protected $forbiddenWords = array();

     public function addForbiddenWord($word) {
         $this->forbiddenWords[] = $word;
     } 

     // Override Zend_Value_Alnum::validate() - I don't know whether such a method even exists 
     // but you know what's the point
     public function validate() {
          parent::validate();

          if (in_array($this->value, $this->forbiddenWords) {
              throw new Exception('Invalid value.');
          }

          return $this->value;
     }
}

// -----------------------

$validator = new MySuperAlnumValidator();
$validator->addForbiddenWord('admin');
$validator->addForbiddenWord('administrator');

$username->addValidator($validator);

This is only a simple example but when you start writing more complex validators/form fields/etc. then objects are, in principle, the only meaningful tool.

Crozin
I think I will stick to building a complete OO solution. As you have outlined it is the best way to handle the complex nature of forms. It will allow the developer to do something outside the box if needed but overriding.
Josh Stuart