PHP code example of maplephp / form
1. Go to this page and download the library: Download maplephp/form 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/ */
maplephp / form example snippets
use MaplePHP\Form\Fields;
use MaplePHP\Form\Examples\TestFormFields; // You should create you own template file for fields
$fields = new Fields(new TestFormFields());
echo $fields->text()->name("email")->label("Email address")->attr([
"type" => "email",
"placeholder" => "Input your email..."
])->get();
$fields->add([
"firstname" => [
"type" => "text", // Set form type (input text or textarea and so on.)
"label" => "First name",
"validate" => [
"length" => [1, 80]
]
],
"lastname" => [
"type" => "text",
"label" => "Last name",
"validate" => [
"length" => [1, 120]
]
],
"email" => [
"type" => "text",
"label" => "Email",
"description" => "We need you email so that we can contact you.",
"attr" => [
"type" => "email",
"placeholder" => "[email protected] "
],
"validate" => [
"length" => [1, 120],
"!email" => NULL
]
],
"nested,item1" => [
"type" => "radio",
"label" => "Question 1",
"validate" => [
"length" => [1],
],
"items" => [
1 => "Yes",
0 => "No"
],
"value" => 1 // Default value
],
"nested,item2" => [
"type" => "radio",
"label" => "Question 2",
"validate" => [
"length" => [1],
],
"items" => [
1 => "Yes",
0 => "No"
],
"value" => 1 // Default value
],
"message" => [
"type" => "textarea",
"label" => "Message",
"validate" => [
"length" => [0, 2000]
]
],
"gdpr" => [
"type" => "checkbox",
//"label" => "GDPR",
"validate" => [
"length" => [1, 1],
"!equal" => [1]
],
"items" => [
1 => "I accept that my data will be saved according to GDPR"
]
]
]);
$fields->setValues([
"firstname" => "John",
"lastname" => "John",
"nested" => [
"item1" => 0,
"item2" => 1,
]
]);
$fields->build();
echo '<form action="index.php" method="post">';
echo $fields->getForm();
echo "</form>";
use MaplePHP\Form\Validate;
$fields->build();
$validate = new Validate($fields, $_POST);
if($error = $validate->execute()) {
// HAS ERROR -->
echo "<pre>";
print_r($error);
echo "</pre>";
} else {
// SUCCESS -->
// Return filtered request (will only return values for added input fields)
$request = $validate->getRequest(); // Uprotected
}