Page 1 of 2

Simple form generator

Posted: Tue Oct 10, 2006 6:12 am
by Jenk
Just spent the last 5-10mins creating this after seeing various threads (both on here and elsewhere) regarding form generators/libraries..

Could probably do with a getter or two, but I've not actually got a genuine use for it (I'm a template slave) so don't know from first hand if these would be valuable.

Comments to be added when I find the effort 8)

Keyword: Simple :p

Code: Select all

<?php

class HTMLForm
{
    private $_members = array();
    private $_properties;
    
    public function __construct($name, $action, $method = 'post')
    {
        $this->_properties['name'] = htmlentities($name);
        $this->_properties['action'] = htmlentities($action);
        $this->_properties['method'] = htmlentities($method);
    }

    public function addMember(HTMLFormMember $member)
    {
        $this->_members[] = $member;
    }
    
    public function setProperty($property, $value)
    {
        $this->_properties[htmlentities($property)] = htmlentities($value);
    }
    
    public function renderHTML()
    {
        $output = '<form';

        foreach ($this->_properties as $name => $val)
        {
            $output .= ' ' . $name . '="' . $val . '"';
        }

        $output .= '>' . chr(10);
        
        foreach ($this->_members as $member)
        {
            $output .= $member->renderHTML() . chr(10);
        }
        
        $output .= '</form>';
        
        return $output;
    }
}

class HTMLFormMember
{
    private $_properties;
    private $_innerHTML;
    private $_type;
    
    public function __construct($name, $type)
    {
        $this->_properties['name'] = htmlentities($name);
        $this->_type = htmlentities($type);
    }
    
    public function setProperty($property, $value)
    {
        $this->_properties[htmlentities($property)] = htmlentities($value);
    }
    
    public function setInnerHTML($text, $escape = true)
    {
        if ($escape) $text = htmlentities($text);
        
        $this->_innerHTML = $text;
    }
    
    public function renderHTML()
    {
        $output = '<' . $this->_type;
        
        foreach ($this->_properties as $property => $value)
        {
            $output .= ' ' . $property . '="' . $value . '"';
        }
        
        if (!is_null($this->_innerHTML))
        {
            $output .= '>' . $this->_innerHTML . '</' . $this->_type . '>';
        }
        else
        {
            $output .= '/>';
        }
        
        return $output . chr(10);
    }
}

$form = new HTMLForm('myForm', '#');

$member = new HTMLFormMember('myTextarea', 'textarea');
$member->setProperty('rows', '2');
$member->setProperty('cols', '30');
$member->setInnerHTML('testing 123... \'"');

$form->addMember($member);

$member2 = new HTMLFormMember('mySubmit', 'input');
$member2->setProperty('type', 'submit');
$member2->setProperty('value', 'click me!');

$form->addMember($member2);

echo $form->renderHTML();

?>
the above example outputs:

Code: Select all

<form name="myForm" action="#" method="post">
<textarea name="myTextarea" rows="2" cols="30">testing 123... '"</textarea>
<input name="mySubmit" type="submit" value="click me!"/>
</form>

Posted: Tue Oct 10, 2006 6:32 am
by volka
What's the bribing advantage over

Code: Select all

<?php
require 'HTML/Quickform.php';

$form = new HTML_QuickForm('myForm');
$form->addElement('textarea', 'myTextarea', null, array('rows'=>2, 'cols'=>20));
$form->addElement('submit', null, 'click me!');
$form->display();
?>
?

Posted: Tue Oct 10, 2006 6:41 am
by Jenk
Didn't realise this was a competition..

But as you asked:

It's modular. You can create an element independantly of the form if you so desire, and if I were to create an HTMLDocument class, this would slot right in 8)

The setProperty method is a lot cleaner and easier to read than

Code: Select all

$form->addElement('textarea', 'myTextarea', null, array('rows'=>2, 'cols'=>20));

Posted: Tue Oct 10, 2006 7:12 am
by volka
Jenk wrote:Didn't realise this was a competition..
For me it always is. It's always the question "Why do you bring in new (generic) code? Why a new framework? Does it solve problems better than the existing code? What problems does it adress?"
Jenk wrote:It's modular. You can create an element independantly of the form if you so desire, and if I were to create an HTMLDocument class, this would slot right in
You can have that with QuickForm, too.
Jenk wrote:The setProperty method is a lot cleaner and easier to read than
For the static part of the form I disagree, but anyway...

Code: Select all

<?php
require 'HTML/Quickform.php';

$form = new HTML_QuickForm('myForm');

/* this can be done by a "new xyz;" statement as well
	but following DOM's createElement ... */
$tf = $form->createElement('textarea', 'myTextarea');
$tf->setRows(2);
$tf->setCols(20);
$form->addElement($tf);

$s = $form->createElement('submit');
$s->setValue('click me!');
$form->addElement($s);

$form->display();
?>

Posted: Tue Oct 10, 2006 7:37 am
by Jenk
What static part? there is no code in your examples to represent the 'static' part of a form.. (Just exactly what is static about a form? :?: )

Anyway.. if you don't like, don't use.. simple as that. If you can see an improvement, then suggest it.

Posted: Tue Oct 10, 2006 9:04 am
by Chris Corbyn
Jenk wrote:Anyway.. if you don't like, don't use.. simple as that. If you can see an improvement, then suggest it.
This is cirtique. People are going to pick and poke at your code as requested :)

Posted: Tue Oct 10, 2006 9:25 am
by Jenk
Indeed, but I don't see what volka posted as critique.. more "Scrap that, use this because I say so." Especially when mentioning a possible flaw but not commenting any furhter on it (static part of form.)

But meh.. like you say, pick and poke away. (Oooer.)

Posted: Tue Oct 10, 2006 10:43 am
by volka
$form = new HTML_QuickForm('myForm');
$form->addElement('textarea', 'myTextarea', null, array('rows'=>2, 'cols'=>20));
$form->addElement('submit', null, 'click me!');
$form->display();
this is static. No matter what the rest of the script does the form always looks the same - there's no dynamic/changing part in it. And in this case I like a compact form of coding. It was not criticism, just a statement, a preference.
Jenk wrote:"Scrap that, use this because I say so."
I just don't understand why I should use this code. I really dislike ad-hoc reinventing the wheel for no good reason.
The reason can be "learning by doing", that's fine with me. But that should imho include dealing with existing works.
I don't tell you to use QuickForm. It's always my (counter-)example, not because it's so brilliant but because it already exists, is hosted at pear.php.net (quite a prominent, central place for a php framework ;)) and has a lot of features. A new framework imho has to do "better" in some way. But most of the time I see direct 1:1 subsets of existing works. That's why I - as end-user of one of the frameworks - asked what the advantage of

Code: Select all

$form = new HTMLForm('myForm', '#');

$member = new HTMLFormMember('myTextarea', 'textarea');
$member->setProperty('rows', '2');
$member->setProperty('cols', '30');
$member->setInnerHTML('testing 123... \'"');

$form->addMember($member);

$member2 = new HTMLFormMember('mySubmit', 'input');
$member2->setProperty('type', 'submit');
$member2->setProperty('value', 'click me!');

$form->addMember($member2);

echo $form->renderHTML();
over

Code: Select all

$form = new HTML_QuickForm('myForm');

/* this can be done by a "new xyz;" statement as well
        but following DOM's createElement ... */
$tf = $form->createElement('textarea', 'myTextarea');
$tf->setRows(2);
$tf->setCols(20);
$form->addElement($tf);

$s = $form->createElement('submit');
$s->setValue('click me!');
$form->addElement($s);

$form->display();
is.

May sound harsh, but don't take it personal.

Posted: Tue Oct 10, 2006 11:17 am
by Jenk
On QuickForm they may be static, but they aren't with the code I posted. :)

Posted: Tue Oct 10, 2006 11:27 am
by volka
pitty we don't understand each other.
new approach:

Code: Select all

$form = new HTMLForm('myForm', '#');

$member = new HTMLFormMember('myTextarea', 'textarea');
$member->setProperty('rows', '2');
$member->setProperty('cols', '30');
$member->setInnerHTML('testing 123... \'"');

$form->addMember($member);

$member2 = new HTMLFormMember('mySubmit', 'input');
$member2->setProperty('type', 'submit');
$member2->setProperty('value', 'click me!');

$form->addMember($member2);

echo $form->renderHTML();
can this code produce different forms?
No, it will always produce the same form, doesn't it?
That's static, isn't it? There's no dynamic part as in e.g.

Code: Select all

$form = new HTMLForm('myForm', '#');

foreach($someArray as $s) {
	$member = new HTMLFormMember($s['name'], 'textarea');
	$member->setProperty('rows', rand(2,10));
	$member->setProperty('cols', time()%100);
}
Just forget about the static/dynamic part. I never mentioned it.

Posted: Tue Oct 10, 2006 3:59 pm
by Jenk
So how exactly does QuickForm differ in that regard?!

Posted: Tue Oct 10, 2006 4:06 pm
by volka
it does not, why should it?
All I said was that you can write it in with quickform this way and that way and I like the short form better for the static skeleton of the form. Nothing more nothing less.

Posted: Wed Oct 11, 2006 3:56 am
by Jenk
You seem to be confusing yourself.. there's nothing static in any of the code produced anywhere, except for what is in the constructors. Really, there isn't.

Code: Select all

<?php

$form = new HTMLForm('myForm', '#');

foreach ($someArray as $s)
{
    $member = new HTMLFormMember($s['name'], 'textarea'); 
    $member->setProperty('rows', rand(2,10)); 
    $member->setProperty('cols', time()%100); 

    $form->addMember($member);
}

echo $form->renderHTML();

?>
Infact you could argue that using setProperty is more flexible than using QuickForm::setRows() etc. But I'm sure QuickForm will also have this extra method (which is one thing I most definitely hold against just about all PEAR lib's - bloat.)

Posted: Wed Oct 11, 2006 4:15 am
by volka
Jenk wrote:You seem to be confusing yourself.. there's nothing static in any of the code produced anywhere, except for what is in the constructors. Really, there isn't.

Code: Select all

<?php

$form = new HTMLForm('myForm', '#');

foreach ($someArray as $s)
{
    $member = new HTMLFormMember($s['name'], 'textarea'); 
    $member->setProperty('rows', rand(2,10)); 
    $member->setProperty('cols', time()%100); 

    $form->addMember($member);
}

echo $form->renderHTML();

?>
ARGH! No this one isn't static. It has a dynamic part. It's depending on $someArray and can produce different forms; it's dynamic, as I said before.
volka wrote:That's static, isn't it? There's no dynamic part as in e.g.

Code: Select all

$form = new HTMLForm('myForm', '#');

foreach($someArray as $s) {
	$member = new HTMLFormMember($s['name'], 'textarea');
	$member->setProperty('rows', rand(2,10));
	$member->setProperty('cols', time()%100);
}
But the previous code

Code: Select all

$form = new HTMLForm('myForm', '#');

$member = new HTMLFormMember('myTextarea', 'textarea');
$member->setProperty('rows', '2');
$member->setProperty('cols', '30');
$member->setInnerHTML('testing 123... \'"');

$form->addMember($member);

$member2 = new HTMLFormMember('mySubmit', 'input');
$member2->setProperty('type', 'submit');
$member2->setProperty('value', 'click me!');

$form->addMember($member2);

echo $form->renderHTML();
always produces the same form; it's static - no matter how it is produced; it's always the same, no dynmic part; it's not dependant on other data; it's static.
I'm really tired of this nonsense . Find someone else to prattle.
Jenk wrote:which is one thing I most definitely hold against just about all PEAR lib's - bloat.)
yes.

Posted: Wed Oct 11, 2006 4:32 am
by Jenk
That's got nothing to do with the code in my first post.. that's down to the implmentation and use of the code.. not the class(es).