1. Go to this page and download the library: Download litepie/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/ */
litepie / form example snippets
use Litepie\Form\Field;
// Simple field
$field = Field::make('text', 'username');
// With fluent API
$field = Field::make('select', 'theme')
->label('Select Theme')
->options(['dark' => 'Dark Mode', 'light' => 'Light Mode'])
->
use Litepie\Form\Fields\TextField;
use Litepie\Form\Fields\SelectField;
$textField = new TextField('username');
$selectField = new SelectField('country');
use Litepie\Form\Facades\Form;
use Litepie\Form\Field;
$contactForm = Form::create()
->action('/contact')
->method('POST')
->add(Field::make('text', 'name')
->label('Full Name')
->quired|email')
)
->add(Field::make('textarea', 'message')
->label('Message')
->
// Tabbed interface
$container->tabbed(true)->activeForm('step1');
// Accordion interface
$container->accordion(true);
// Stacked interface (default)
// Forms displayed one after another
// Individual validation (default) - each form validated separately
$container->validationMode('individual');
// Combined validation - all forms must pass
$container->validationMode('combined');
// Sequential validation - stops at first failure
$container->validationMode('sequential');
class RegistrationContainer extends \Litepie\Form\FormContainer
{
public function __construct($app)
{
parent::__construct($app, 'registration');
$this->setupRegistrationForms();
}
protected function setupRegistrationForms(): void
{
$this->name('User Registration')
->tabbed(true)
->validationMode('sequential');
// Step 1: Personal Information
$personal = $this->createForm('personal', [
'title' => 'Personal Information',
'icon' => 'user'
]);
$personal
->add('first_name', 'text', [' return (int)(($currentStep + 1) / $totalSteps * 100);
}
}
// Usage
$registrationContainer = new RegistrationContainer(app());
echo $registrationContainer->render();
class ContactController extends Controller
{
public function create()
{
$form = Form::create()
->action(route('contact.store'))
->method('POST')
->add('name', 'text', ['uest)
{
// Form validation is automatic
$validated = $request->validate([
'name' => '
// Convert existing form to array
$form = Form::create()
->add('name', 'text', ['formJson = $form->toJson();
// Or use helper functions
$formArray = form_array([
'name' => ['type' => 'text', 'label' => 'Name', '
// Return form schema as JSON for Vue/React/Angular
Route::get('/api/forms/contact', function() {
return form_array([
'name' => ['type' => 'text', 'label' => 'Name', ' 'action' => '/api/contact',
'method' => 'POST'
]);
});
class ContactFormTest extends TestCase
{
/** @test */
public function it_validates_contact_form()
{
$form = Form::create()
->add('name', 'text', [''
]));
$this->assertFalse($form->validate([
'name' => '',
'email' => 'invalid-email'
]));
}
}
$form->action(string $action) // Set form action URL
$form->method(string $method) // Set HTTP method (POST, GET, etc.)
$form->files(bool $enabled = true) // Enable file uploads
$form->theme(string $theme) // Set UI framework (bootstrap5, tailwind)
$form->ajax(bool $enabled = true) // Enable AJAX submission
$form->multiStep(bool $enabled = true) // Enable multi-step forms
$form->forUser(object $user) // Set user for visibility checks
$form->getUser() // Get current user
$form->cache(int $ttl = 3600) // Enable caching with TTL
$form->cached() // Check if caching is enabled
$form->withoutCache() // Disable caching
$form->clearCache() // Clear form cache
$form->add(string $name, string $type, array $options = []) // Add field
$form->remove(string $name) // Remove field
$form->has(string $name) // Check if field exists
$form->get(string $name) // Get field instance
$form->row(array $fields, ?string $id = null) // Add fields in a row
$form->group(string $id, ?string $title = null, ?string $description = null) // Start group
$form->section(string $id, ?string $title = null, ?string $description = null) // Start section
$form->endGroup() // End current group
$form->endSection() // End current section
$form->divider(?string $label = null) // Add divider
$form->defaultWidth(int $width) // Set default field width
$form->populate(array $data) // Populate form with data
$form->validate(array $data) // Validate form data
$form->getValidationRules() // Get all validation rules
$form->render(?object $user = null) // Render HTML (respects visibility)
$form->toArray(?object $user = null) // Convert to array (respects visibility)
$form->toJson(?object $user = null) // Convert to JSON (respects visibility)
$form->visibleFields(?object $user = null) // Get visible fields only
$form->renderField(string $name) // Render single field
$form->renderErrors() // Render validation errors