PHP code example of scratchwizard1 / form-generator

1. Go to this page and download the library: Download scratchwizard1/form-generator 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/ */

    

scratchwizard1 / form-generator example snippets



FormGenerator\Form;

use FormGenerator\Form;

$form = Form::new("index.php", "contact-form");

$form->fieldset()
     ->class("fieldset-class")
     ->name("main");

$form->legend("Contact us");

$form->text("name")
     ->placeholder("Your name")
     ->minlength(3)
     ->

$form->text("username")->

$form->button("myBtn", "Click Me");

$form->image("submitImage", "/img/send.png", "Send");

$form->html("<p>Custom content</p>")->class("box");

$form->text("username")->min(5);

$form->text("username")
     ->setAttributes([
         'data-test' => 'example',
         'aria-label' => 'Username field'
     ]);

$form->text("username")
     ->id("secondaryId")
     ->setAttributes(['id' => 'primaryId']);

'data-user' => '123'
'aria-label' => 'Form'
'custom-attribute' => 'value'

$form->text("username")
     ->setAttributes(['

$form->text("username")
     ->setAttributes(['

use FormGenerator\Form;

$result = Form::getDataFrom("contact-form", [
    'name'  => 'string;; // associative array of sanitized values
} else {
    $error = $result->getError(); // returns one error at a time
}

use FormGenerator\Form;

$result = Form::getDataFrom("", [
    'name'  => 'string;t->getData(); // associative array of sanitized values
} else {
    $error = $result->getError(); // returns one error at a time
}

use FormGenerator\Form;

$result = Form::getDataFrom("contact-form");

$data = $result->getData(); // all submitted data, no validation applied

[
    'name'  => 'Jane Doe',
    'email' => '[email protected]'
]

[
    'message' => "Validation error for key 'name': The value has length 2 and is below the allowed minimum.",
    'code'  => 422,
    'id'    => 'vE084',
    'file'  => '/var/www/html/index.php',
    'line'  => 42
]

$result = Form::getDataFrom("contact-form", [
    '__CONFIG__' => 'strip_tags:true; htmlspecialchars:true',
    'name'       => 'string;

'__CONFIG__' => 's:t; h:t'   // enable both strip_tags and htmlspecialchars (default)
'__CONFIG__' => 's:f; h:f' // disable both (raw values)
'__CONFIG__' => 's:t; h:f'   // only strip tags, no HTML escaping

use FormGenerator\Validation;

$data = [
    'username' => 'janedoe',
    'email'    => '[email protected]',
    'age'      => '25'
];

$result = Validation::validate($data, [
    'username' => ['string', 'req', 'minl:3', 'maxl:20'],
    'email'    => 'email;req',
    'age'      => ['number:i', 'min:18', 'max:100']
], true);

if ($result->isValid()) {
    $validData = $result->getData();
} else {
    $errors = $result->getError();
}

Form::captchaInit(
    'your-project-id',
    'your-site-key',
    '/config/google-recaptcha.json' // path to the JSON credentials file
);