Tuesday, September 11, 2012

HTML form element class object in PHP

Form Elements as easy as

$elem = new FormElement("yourElement", "Hello World Element");
echo $elem->HTML();

This Will print
--------------------------
<div class=''>Hello World Element</div>
<input id="yourElement" name="yourElement" type="text" form="form" onblur="Validate('yourElement')"/>
<div name='err' class='error'></div>
--------------------------
The last div named 'err' can be used to display errors from the validation.


<?php

/**
 * Description of FormElement
 * Form Element HTML Object
 * @author Joseph Tveter
 *
 * $name        is the name and id of the form element.
 * $cmd         is the HTML tag
 * $label       is the Label the User will see for the field the default is that it will not be there.
 * $pos         is the position of the label.  The default is "top", but it will also accept "right", "left", and "bottom"
 * $formName    is the name of the form the element is attached to. The default is "form"
 * $type        is the type of input tag it is. The default is "text"
 * $onblur      is the javascript function called onblur. default will call the function called "Validate" with the name of the field.  false will not include this field.
 * $val         is the value the tag has or will return.  The default is ""
 * $req         is weather to make the field required or not. The default is false
 * $disabled    is weather to make the field disabled or not. The default is false
 * $size        is the size of the input field. The default is ""
 * $maxLength   is the Maximum Length of the input field. The default is ""
 * $fullclass   is the class assigned to the div wrapper around the label and form element. The default is ""
 * $titleClass  is the class assigned to the label. The default is ""
 * $elementClass is the class of the form element.  The default is ""
 */
class FormElement extends HTML
{
    public function __construct($name, $label = "", $cmd = "input", $pos = "top", $formName = "form", $type = "text", $onblur = "default", $val = "", $req = false, $disabled = false, $size = "", $maxLength = "", $elementClass = "", $fullclass = "", $titleClass = "")
    {
        parent::__construct("div");
        if($fullclass != "")
        {
            parent::addValue("class", $fullclass);
        }
     
        $input = new HTML($cmd);
        $input->addValue("id", $name);
        $input->addValue("name", $name);
        $input->addValue("type", $type);
        $input->addValue("form", $formName);
     
        if($onblur != "default")
        {
            if($onblur != false)
            {
                $input->addValue("onblur", $onblur);
            }
        }
        else
        {
            $input->addValue("onblur", "Validate('$name')");
        }
     
        if($val != "")
        {
            $input->addValue('value', $val);
        }
     
        if($disabled == true)
        {
            $input->addValue('disabled', "disabled");
        }
     
        if($req == true)
        {
            $input->addValue("required", "required");
        }
     
        if($size != "")
        {
            $input->addValue("size", $size);
        }
     
        if($maxLength != "")
        {
            $input->addValue("maxlength", $maxLength);
        }
     
        if($elementClass != "")
        {
            $input->addValue("class", $elementClass);
        }
     
     
        if($label != "")
        {
            switch($pos)
            {
                case "":
                    parent::addChild("<div class='$titleClass'>$label</div>");
                    parent::addChild($input->HTML());
                    parent::addChild("<div name='err' class='error'></div>");
                break;
         
                case "top":      
                    parent::addChild("<div class='$titleClass'>$label</div>");
                    parent::addChild($input->HTML());
                    parent::addChild("<div name='err' class='error'></div>");
                break;
         
                case "bottom":
                    parent::addChild($input->HTML());
                    parent::addChild("<div class='$titleClass'>$label</div>");
                    parent::addChild("<div name='err' class='error'></div>");
                break;
         
                case "right":
                    $input->addChild("<span class='$titleClass'>$label</span>");
                    parent::addChild($input->HTML());
                    parent::addChild("<div name='err' class='error'></div>");
                break;
         
                case "left":
                    parent::addChild("<label for='$name' class='$titleClass'>$label</label>");
                    parent::addChild($input->HTML());
                    parent::addChild("<div name='err' class='error'></div>");
                break;
            }
        }
        else
        {
            parent::addChild($input->HTML());
        }
    }
}

?>

No comments:

Post a Comment