SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Sweden
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Help me with my FormGenerator class

    Ah, this feels great. Finnaly I have taken the step to "show" myself here at the SitePoint forums. I have been a member for some time now but this is actually my first post (except for the presentation post I wrote ages ago).

    Well anyways, let's go straight to the point. I have begun writing something I call a "FormGenerator". What I want this application to do is to help me create forms on my websites in a better and easier way. My thoughts is that it - in the future - also will be able to work together with some kind of FormValidation class and maybe some other classes.

    Anyways (again) I want some opinions on the class. Is it good or bad, what can I make better, what do you like and dislike about it, should I continue on it or should I throw it in my recycle bin?

    I also want to some help here, I have tried to figure out a way to be able to set "selected" attribute on the option-elements in the select-list but I don't know how to do it.

    Also I don't like the way I output the form right now. I don't really like that it prints out every element for itself. When I first started coding on this class my plan was to use some kind of output function to print the hole form directly after everything have been set up, but that didn't work very well becuase then I couldn't layout the form as I want with divs and such.

    So I want tips and trix on that to

    Well here is the code:

    And by the way, sorry about my english. I haven't used english in a long time so you'll have to accept my school-english

    PHP Code:
    <?php
        
    /**
         * Generates a form with elements
         * 
         */
        
    class FormGenerator
        
    {
            
    /**
             * Gives the form a unique id
             */
            
    private $form_id;
            
            
    /**
             */
            
    private $form_data;
            
            
    /**
             * Decides if the FormGenerators output ($form_data) should be returned (saved) or printed
             */
            
    private $output;
            
            
    /**
             * Contructs a FormGenerator object
             */
            
    public function __construct($output 'return')
            {
                
    $this->output $output;
            }
            
            
    /**
             * Destructs a FormGenerator object
             */
            
    public function __destruct()
            {
                
            }
            
            
    /**
             * Sets up a container for a form tag
             */
         
    public function openFormContainer($method$name$title$extra$enctype='application/x-www-form-urlencoded')
            {
             
    $this->form_data '<form enctype="'.$enctype.'" method="'.$method.'" name="'.$name.'" title="'.$title.'"'.$extra.'>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Finishes a container for a form tag
             */
            
    public function closeFormContainer()
            {
                
    $this->form_data '</form>' "\n";
                print 
    $this->form_data;
                
            }
            
            
    /**
             * Groups related form elements
             */
            
    public function openFieldset($extra=null)
            {
                
    $this->form_data '<fieldset'.$extra.'>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             */
            
    public function closeFieldset()
            {
                
    $this->form_data '</fieldset>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Specifies a description for a fieldset
             */
            
    public function addLegend($align='top'$value$extra=null)
            {
             
    $this->form_data '<legend align="'.$align.'"'.$extra.'>'.$value.'</legend>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Provides identifying text for a form widget
             */
            
    public function addLabel($for$value$extra=null)
            {
             
    $this->form_data '<label for="'.$for.'" title="'.$title.'"'.$extra.'>'.$value.'</label>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Identifies several input methods for forms
             */
            
    public function addInput($name$type$extra=null)
            {
             
    $this->form_data '<input name="'.$name.'" type="'.$type.'"'.$extra.' />' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Specifies a selection list within a form
             */
            
    public function addSelect($name$options$extra=null)
            {
                
    $this->form_data '<select name="'.$name.'"'.$extra.'>' "\n";
                
                foreach (
    $options as $option_value => $option_item)
                {
              
    $this->form_data "\t" '<option value="'.$option_value.'"'.$extra.'>'.$option_item.'</option>' "\n";
                }
                
                
    $this->form_data '</select>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Defines a multiple-line text input field within a form
             */
            
    public function addTextarea($name$value=null$extra=null)
            {
             
    $this->form_data '<textarea name="'.$name.'"'.$extra.'>'.$value.'</textarea>' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Sets up a button to submit or reset a form as well as to activate a script
             */
            
    public function addButton($name$type$value$extra=null)
            {
             
    $this->form_data '<button name="'.$name.'" type="'.$type.'" value="'.$value.'"'.$extra.' />' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             * Sets up a premade submit button
             */
            
    public function addSubmitButton($name$value$extra=null)
            {
             
    $this->form_data '<input name="'.$name.'" type="submit" value="'.$value.'"'.$extra.' />' "\n";
                print 
    $this->form_data;
            }
            
            
    /**
             */
            
    public function output()
            {
                if (
    $this->output == 'print')
                {
                    print 
    $this->form_data;
                    return 
    1;
                }
                elseif (
    $this->output == 'return')
                {
                    return 
    $this->form_data;
                }
            }
            
        }
        
        
    $form = new FormGenerator('print');
        
        
    $form->openFormContainer('post''myTestForm''Fill in my test form''');
        
        
    $form->openFieldset();
        
    $form->addLegend('top''My Test Form');
        
        print 
    '<p>' "\n";
        
    $form->addLabel('first_name''First Name');
        
    $form->addInput('first_name''text');
        print 
    '</p>' "\n";
        
        print 
    '<p>' "\n";
        
    $form->addLabel('last_name''Last Name');
        
    $form->addInput('last_name''text');
        print 
    '</p>' "\n";
        
        print 
    '<p>' "\n";
        
    $form->addLabel('password''Password');
        
    $form->addInput('password''password');
        print 
    '</p>' "\n";
        
        
    $console_options = array (
            
    'nes' => 'Nintendo 8-bit',
            
    'snes' => 'Super Nintendo (16-bit)',
            
    'saturn' => 'Sega Saturn',
            
    'playstation' => 'Playstation',
            
    'dreamcast' => 'Dreamcast',
            
    'n64' => 'Nintendo 64'
        
    );
        
        print 
    '<p>' "\n";
        
    $form->addSelect('consoles_you_own'$console_options);
        print 
    '</p>' "\n";
        
        
    $form->closeFieldset();
        
        
    $form->addSubmitButton('send_data''Login to My Realm');
        
        
    $form->closeFormContainer();
        
        
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I apologize if your intent is to create your own form template class instead of using another but have you looked at the pear package HTML_FORM?

  3. #3
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Sweden
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips pworley. I guess my intent was/is to create my own class, cause I pretty much want my own classes and functions for everything. It's easier to work with homemade stuff. But I'll take a look at the PEAR package, if I don't like at least it will give me ideas.

    I don't know much about PEAR really so I have a question or two.
    Does every PHP installation support PEAR?
    Is there a good chance that my webhost has support for PEAR?

    Well now I have taken a good look at the PEAR-library and well, it's not really what I want but it sure gave me a lot of ideas so now I have re-structured pretty much everything about the class. It's now two classes btw, but anyways. This thread is pretty dead. If I want any comments on my other class in the future I'll create a new thread
    Last edited by Teddy; Apr 24, 2005 at 07:37. Reason: The update is not important enough to be on another post


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •