devture/form
Laravel form builder for constructing, validating, and rendering HTML forms using a structured API. Helps define fields, rules, and layout in PHP and output consistent form markup for your views.
Installation Add the package via Composer:
composer require devture/form
No service provider or package discovery needed—just autoload.
First Use Case: Basic Form Binding Define a form class:
use Devture\Form\Form;
class UserForm extends Form
{
public string $name;
public int $age;
}
Bind request data:
$form = new UserForm();
$form->bind($_POST); // or request()->all() in Laravel
Where to Look First
Form class docs (methods: bind(), validate(), errors())Validation trait (if extended)$form = new UserForm();
$form->bind(request()->all());
if ($form->validate()) {
// Process data
$form->save(); // Custom method
}
@foreach ($form->errors() as $field => $messages)
<div>{{ $field }}: {{ implode(', ', $messages) }}</div>
@endforeach
FormRequest and use the form for validation:
public function rules()
{
$form = new UserForm();
$form->bind($this->all());
return $form->rules(); // Hypothetical; check actual API
}
Blade::directive('form', function ($formClass) {
return "<?php \$form = new {$formClass}(); \$form->bind(request()->all()); ?>";
});
Usage:
@form(UserForm)
<input name="name" value="{{ $form->name }}">
Define rules in the form class:
class UserForm extends Form
{
public string $email;
protected function rules(): array
{
return [
'email' => 'required|email',
];
}
}
FormRequest, this package doesn’t handle CSRF tokens. Add manually:
if (!hash_equals($_POST['_token'], csrf_token())) {
abort(419);
}
"123" → int). Cast explicitly:
$form->age = (int) $form->age;
messages() method (if supported):
protected function messages(): array
{
return [
'email.required' => 'We need your email address!',
];
}
dd($form->toArray()); // Hypothetical; check actual API
$form->validate(['name' => 'John', 'age' => '30']);
Form class to add logic:
class UserForm extends Form
{
public function validateAge()
{
if ($this->age < 18) {
$this->addError('age', 'Must be 18+');
}
}
}
bind() to sanitize input:
public function bind(array $data): void
{
$data = array_map('trim', $data);
parent::bind($data);
}
protected $fillable = ['name', 'email'];
.env or config/form.php.rules() method exists, the form assumes no validation. Define explicitly.How can I help you explore Laravel packages today?