PHP code example of hassanalthaf / formbuildercomponent

1. Go to this page and download the library: Download hassanalthaf/formbuildercomponent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hassanalthaf / formbuildercomponent example snippets


use \HassanAlthaf\Form;

$formBuilder = new FormBuilders\HtmlFormBuilder();

// Now, you need to define the attributes for the <form> element.

$formBuilder->newForm(['method' => 'post', 'action' => '']);

// Saving the form with a name to uniquely identify it. Note that it doesn't affect the name attribute of the form.

$formBuilder->saveForm("testForm");

// Showing the form.

echo $formBuilder->buildMarkup("testForm");

$labelTest = new FormElements\HtmlFormElements\HtmlLabel();
$labelTest->addAttribute("for", "password");
$labelTest->setDisplayName('Password');

$formBuilder->addElement($labelTest, 'labelTest');

$passwordField = new FormElements\HtmlFormElements\HtmlInputField();
$passwordField->addAttribute("type", "password");
$passwordField->addAttribute("name", "password");
$passwordField->addAttribute("placeholder", "Password");
$passwordField->addAttribute("id", "password");

$formBuilder->addElement($passwordField, 'passwordField');

$textarea = new FormElements\HtmlFormElements\HtmlTextArea();
$textarea->setText("Example");
$textarea->addAttribute('cols', '5');
$textarea->addAttribute('rows', '5');

$formBuilder->addElement($textarea, 'textareaTest');

$submitButton = new FormElements\HtmlFormElements\HtmlButton("Login");
$submitButton->addAttribute("name", "login");

$formBuilder->addElement($submitButton, 'submitButton');

/* Creating a radio button with two radio button options: $maleField and $femaleField */
$radioButton = new FormElements\HtmlFormElements\HtmlRadioButton("Gender");
$maleField = new FormElements\HtmlFormElements\HtmlInputField();
$maleField->addAttribute("value", "Male");
$maleField->addAttribute("type", "radio");

$femaleField = new FormElements\HtmlFormElements\HtmlInputField();
$femaleField->addAttribute("value", "Female");
$femaleField->addAttribute("type", "radio");

/* Requires exception to be caught */
try {
    $radioButton->addField($maleField, "male");
    $radioButton->addField($femaleField, "female");
} catch (\Exception $ex) {
    print_r($ex);
}

/* Creating a dropdownlist with 3 different list elements */
$dropDownList = new FormElements\HtmlFormElements\HtmlDropDownList();
$dropDownList->addAttribute("name", "vehicles");

$bugattiListElement = new FormElements\HtmlFormElements\HtmlListElement();
$bugattiListElement->setDisplayValue("Bugatti");
$bugattiListElement->addAttribute("value", "bugatti");

$lamborghiniListElement = new FormElements\HtmlFormElements\HtmlListElement();
$lamborghiniListElement->setDisplayValue("Lamborghini");
$lamborghiniListElement->addAttribute("value", "lamborghini");

$ferrariListElement = new FormElements\HtmlFormElements\HtmlListElement();
$ferrariListElement->setDisplayValue("Ferrari");
$ferrariListElement->addAttribute("value", "ferrari");

$dropDownList->addListElement($bugattiListElement, "bugatti");
$dropDownList->addListElement($lamborghiniListElement, "lamborghini");
$dropDownList->addListElement($ferrariListElement, "ferrari");

$formBuilder->addElement($dropDownList, 'dropDownList');

$formBuilder->edit("testForm");
$formBuilder->removeElement('textareaTest');
$formBuilder->saveForm("testForm");

echo $formBuilder->buildMarkup("testForm");