Jump to content

OOP + Forms, need some advice.


AzizLight

Recommended Posts

Hi everybody,

I followed the OOP PHP course on this site which was very helpful. I'm now working in a small company that uses a set of functions to dynamically create forms. So for instance they have a function called beginForm that takes two arguments (the form action and the legend tag content - the title of the form) and that creates the form and fieldset opening tags and the legend tag. And this way the programmer creates forms my calling a set of functions. Now my job is to improve that system by porting it to OOP.

Now since this is my first OOP PHP project (and for that matter my first PHP project too) I need some advice on how to handle this. Should I just create one class instead of each function and extend a base "input" class to create several types of input fields or should I create one big class (in which case I have no idea how to proceed), or should I proceed in another way? The idea is to be able to easily create forms dynamically so that we dont have to retype the code everytime we want to add a form (and ultimately let a non-programmer create forms using a form - just a random idea...).

What do you thnk?

Link to comment
Share on other sites

a little up!:rolleyes:

 

after a couple days of researching and testing, here is what I ended up having:

 

<?php

// Error Detection system
error_reporting( E_ALL | E_STRICT ); ini_set( 'display_errors', true );

/**
* Dynamically create a form
*/
class Form
{
   private $action, $method;
   public function __construct($action, $method)
   {
       $this->action = $action;
       $this->method = $method;
   }

   /**
    * Render the dynamic form
    *
    * @return string
    **/
   public function render($inputs)
   {
       $output = '';

       foreach ($inputs as $input) {
           $output .= $input->render();
       }

       $output .= '';
       return $output;
   }
}

/**
* Parent Class for form objects
*/
abstract class FormObject
{
   public $id, $class, $value;
   function __construct($id=null, $value=null, $class=null)
   {
       // NOTE - J'ai enleve le parametre $name, puisqu'il a tout le temps la meme valeur que $id
       $this->id   = $id;
       $this->value = $value;
       $this->class = $class;
   }

   abstract function render();
}

/**
* Input Fields: A Type of FormObject
*/
class InputField extends FormObject
{
   public $type, $checked;
   // NOTE - The checked option is for the radio type inputs
   public function __construct($type, $id, $value=null, $checked=null, $class=null)
   {
       parent::__construct($id, $value, $class);
       $this->type    = $type;
       $this->id      = $id;
       $this->class   = $class;
       $this->value   = $value;
       $this->checked = $checked;
   }

   /**
    * Generate the form element
    *
    * @return string
    **/
   public function render(){
       // NOTE - Dans toutes les methodes render() j'utilise des if et pas de elseif parce qu'il s'agit de conditions independantes
       $output = '        if ($this->checked != null) {
           $output .= ' checked="'.$this->checked.'"';
       }
       if($this->value != null) {
           $output .= ' value="'.$this->value.'"';
       }
       if ($this->class != null) {
           $output .= ' class="'.$this->class.'"';
       }
       $output .=  ' />';
       return $output;
   }
}

/**
* Textarea, Dropdown menus, labels
*/
class TaDdL extends FormObject
{
   public $tag, $for, $size, $options;
   public function __construct($tag, $id=null, $value=null, $for=null, $options=null, $size=null, $class=null)
   {
       parent::__construct($id, $value ,$class);
       $this->tag   = $tag;
       $this->id    = $id;
       $this->for   = $for;
       $this->size = $size;
       $this->class = $class;
       $this->value = $value;
       $this->options = $options;
   }

   /**
    * Generate the form element
    *
    * @return string
    **/
   public function render(){
       $output = '<'.$this->tag;

       if ($this->id != null) {
           $output .= ' id="'.$this->id.'"';
       }
       if ($this->size != null) {
           $output .= ' size="'.$this->size.'"';
       }
       if ($this->for != null) {
           $output .= ' for="'.$this->for.'"';
       }
       if ($this->class != null) {
           $output .= ' class="'.$this->class.'"';
       }

       // ici je met la valeur entre les deux tags et non a l'interieur du tag d'ouverture donc je suis oblige de fermer le tag d'ouverture, d'ou le else
       if ($this->value != null) {
           $output .= '>'.$this->value.''.$this->tag.'>';
       } elseif ($this->options != null) {
           $output .= '>';
           foreach ($this->options as $value => $option) {
               $output .= ''.$option.'';
           }
           $output .= ''.$this->tag.'>';
       } else {
           $output .= '>'.$this->tag.'>';
       }
       return $output;
   }
}

?>

 

This code works pretty well but in my opinion it doesn't have anything that makes it "superior" to a regular collection of functions, it's just re-rewritten in "the oop way".

 

So can anybody give me any suggestions on how I could improve that code in anyway so that it makes it significantly better than a regular collection of precedural php functions?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...