Location: PHPKode > projects > Moc10 PHP Library > application/Test/Form/Contact.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Form
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Test_Form_Contact
 *
 * @category   Moc10
 * @package    Moc10_Form
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Test_Form_Contact extends Moc10_Form
{

    /**
     * Contact form template
     * @var string
     */
    protected $_template = null;

    /**
     * $_POST fields
     * @var array
     */
    protected $_postFields = array('name' => null,
                                   'email' => null,
                                   'gender' => null,
                                   'subscribe' => null,
                                   'colors' => null,
                                   'comments' => null);

    /**
     * Initialization method for the test contact form.
     *
     * @param  array $post
     * @return void
     */
    public function init($post = null)
    {

        $this->_setFields($post);
        $this->_setTemplate();

        $this->setAttributes('id', 'loginForm');

        // Create the 'name' form element.
        $name = new Moc10_Form_Element('text', 'name', $this->_postFields['name']);
        $name->setLabel('Name:');
        $name->setAttributes('size', '40');
        $name->addValidator('AlphaNum');
        $name->setRequired(true);

        // Create the 'email' form element.
        $email = new Moc10_Form_Element('text', 'email', $this->_postFields['email']);
        $email->setLabel('Email:');
        $email->setAttributes('size', '40');
        $email->addValidator('Email');
        $email->setRequired(true);

        // Create the 'gender' select form element.
        $gender = new Moc10_Form_Element_Select('gender', array('----' => '----', 'Male' => 'Male', 'Female' => 'Female'), $this->_postFields['gender']);
        $gender->setLabel('Gender:');
        $gender->addValidator('NotEqual', true, '----');
        $gender->setAttributes('class', 'selectField');
        $gender->setAttributes('onchange', "doSomeMoreJS('loginForm');");

        // Create the 'subscribe' radio form element.
        $subscribe = new Moc10_Form_Element_Radio('subscribe', array('Yes' => 'Yes', 'No' => 'No', 'Maybe' => 'Maybe', 'IDK' => 'I Dont Know, OK!'), $this->_postFields['subscribe']);
        $subscribe->setLabel('Subscribe:');

        // Create the 'colors' checkbox form element.
        $colors = new Moc10_Form_Element_Checkbox('colors', array('Red' => 'Red', 'White' => 'White', 'Blue' => 'Blue'), $this->_postFields['colors']);
        $colors->setLabel('Colors:');

        // Create the 'comments' textarea form element.
        $comments = new Moc10_Form_Element_Textarea('comments', $this->_postFields['comments'], null);
        $comments->setLabel('Questions/Comments:');
        $comments->setAttributes('rows', '5');
        $comments->setAttributes('cols', '40');
        $comments->setRequired(true);

        // Create the 'submit' form element.
        $submit = new Moc10_Form_Element('submit', 'submit', 'Send');
        $submit->setAttributes('class', 'submitBtn');

        // Set the form template and add the form element objects to the form.
        $this->setTemplate($this->_template);
        $this->addElements(array($name, $email, $gender, $subscribe, $colors, $comments, $submit));

    }

    /**
     * Method to process the form data.
     *
     * @return void
     */
    public function process()
    {

        // Form processing code can go here.
        print('The form has been successfully processed.');

    }

    /**
     * Method to set the post fields.
     *
     * @param  array $post
     * @return void
     */
    protected function _setFields($post = null)
    {

        if (!is_null($post)) {
            foreach ($post as $key => $value) {
                if (array_key_exists($key, $this->_postFields)) {
                    $this->_postFields[$key] = (isset($post[$key])) ? $post[$key] : null;
                }
            }
        }

    }

    /**
     * Method to set the form template.
     *
     * @return void
     */
    protected function _setTemplate()
    {

        // Define the form template.
        $this->_template = <<<FORMTEMP
        <table>
            <tr>
                <td>
                    [{name_label}]
                </td>
                <td>
                    [{name}]
                </td>
            </tr>
            <tr>
                <td>
                    [{email_label}]
                </td>
                <td>
                    [{email}]
                </td>
            </tr>
            <tr>
                <td>
                    [{gender_label}]
                </td>
                <td>
                    [{gender}]
                </td>
            </tr>
            <tr>
                <td>
                    [{subscribe_label}]
                </td>
                <td>
                    [{subscribe}]
                </td>
            </tr>
            <tr>
                <td>
                    [{colors_label}]
                </td>
                <td>
                    [{colors}]
                </td>
            </tr>
            <tr>
                <td>
                    [{comments_label}]
                </td>
                <td>
                    [{comments}]
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    [{submit}]
                </td>
            </tr>
        </table>
FORMTEMP;

    }

}
Return current item: Moc10 PHP Library