tags:

views:

1405

answers:

4

How would I go about creating a real world form creation class that I can use to display a new form with fields of different types, as how many fields I want, I can use drop downs and I can do all of this by using OOP?

+4  A: 

You definitely can. Consider a Form class which stores information about the form itself: the method, action, enctype attributes. Also throw in stuff like an optional heading and/or description text at the top. Of course you will also need an array of input elements. These could probably be put into their own class (though subclassing them for InputText, InputCheckbox, InputRadio maybe be a bit over the top). Here's a vague skeleton design:

class Form {
    var $attributes,    // array, with keys ['method' => 'post', 'action' => 'mypage.php'...]
        $heading,
        $description,
        $inputs        // array of FormInput elements
    ;

    function render() {
        $output = "<form " . /* insert attributes here */ ">"
              . "<h1>" . $this->heading . "</h1>"
              . "<p>" . $this->description . "</p>"
        ;
        // wrap your inputs in whatever output style you prefer:
        // ordered list, table, etc.
        foreach ($this->inputs as $input) {
            $output .= $input->render();
        }
        $output .= "</form>";
        return $output;
    }
}

The FormInput class would just need to store the basics, such as type, name, value, label. If you wanted to get tricky then you could apply validation rules which would then be converted to Javascript when rendering.

nickf
+4  A: 

To be honest I wouldn't roll my own, considering there are a few mature form packages out there for PHP.

I use PEAR's HTML_QuickForm package (http://pear.php.net/manual/en/package.html.html-quickform.php) for PHP4 sites.

For PHP5, I'd have a look into Zend_Form (http://framework.zend.com/manual/en/zend.form.html).

For my quickform code, I use a helper class that lets me define forms using a config array. For example:

echo QuickFormHelper::renderFromConfig(array(
  'name' => 'area_edit',
  'elements' => array(
    'area_id'           => array('type' => 'hidden'),
    'active'            => array('type' => 'toggle'),
    'site_name'         => array('type' => 'text'),
    'base_url'          => array('type' => 'text'),
    'email'             => array('type' => 'text'),
    'email_admin'       => array('type' => 'text'),
    'email_financial'   => array('type' => 'text'),
    'cron_enabled'      => array('type' => 'toggle'),
    'address'           => array('type' => 'address'),
  ),
  'groups' => array(
    'Basic Details'   => array('site_name', 'base_url'),
    'Address Details' => array('address'),
    'Misc Details'    => array(), // SM: Display the rest with this heading.
  ),
  'defaults' => $site,
  'callback_on_success' => array(
    'object' => $module,
    'function' => 'saveSite',
   ),
));

Note that the above element types 'address' and 'toggle' are in fact multiple form fields (basically, meta-types). This is what I love about this helper class - I can define a standard group of fields with their rules (such as address, credit_card, etc) and they can be used on lots of pages in a consistent fashion.

starmonkey
A: 

thanks for your input guys especially nickf

Keith Donegan
no problem, but by going with nickf's example you'll be reinventing the wheel. enjoy!
starmonkey
A: 

j ai des class dans mon projet class stagiaire + les getters et les setters et un controleur qui contient une methode ajouter_stagiaire(Stagiaire $s)

un formulaire de l'ajoute des stagiaire . le qst comment je dois faire pour appeler le methode ajouter_stagiaire(s) dans mon formulaire.

et merci d'avance

yassin