Create advanced, consistent and secure forms and validations.
use MaplePHP\Form\Fields;
use MaplePHP\Form\Examples\TestFormFields; // You should create you own template file for fields
$fields = new Fields(new TestFormFields());
It is recommended that you make a copy off AbstractFormFields class, make it to a regualar class, rename it and extend it to the real AbstractFormFields abstract class. Then you can start making and adding your own custom input fields.
$fields->[FIELD_TYPE]->[ARG1]->[ARG2]->get(); FIELD_TYPE: Method name from Form\Templates\Fields ARG: Chainable arguments like input name, fields attributes, validations and so on.
echo $fields->text()->name("email")->label("Email address")->attr([
"type" => "email",
"placeholder" => "Input your email..."
])->get();
Use the form compiler for advance consistent form creation and validation. Works really great in frameworks and large applications.
[
inputFieldName => [
// Field config…
],
…
…
]
Expects defined form type key Example: text, textarea, date, select, checkbox, radio and more. Required
Define a input label Example: Email address
Define a input label Example: We need your email to…
Add html attributens to field Example:
[
class => inp-email,
type => email,
placeholder => Fill in the email
]
Add checkbox, radio or select list items. Example:
[
1 => Yes,
0 => No
]
Is required for field types like select, checkbox and radio.
Add validation to form field Example:
[
length => [1, 200],
!email => NULL
]
The exclamation point before the email key means that it will only validate email if it is filled in else skip or do the other validation.
Pass on custom data for a custom field. Example:
[
role => admin,
user_id => 5212
]
Build a whole form with array as bellow
$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"
]
]
]);
If you have values from for example the database (accepts multidimensional array and object)
$fields->setValues([
"firstname" => "John",
"lastname" => "John",
"nested" => [
"item1" => 0,
"item2" => 1,
]
]);
You will allways need to build the form before read or validations.
$fields->build();
Now you can read the form.
echo '<form action="index.php" method="post">';
echo $fields->getForm();
echo "</form>";
Now you can read the 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
}