vkm-apps/x-form
Laravel package for building and managing forms with an expressive API. Helps define fields, validation, and rendering in a structured way, simplifying form creation and reuse across your application.
Installation:
composer require vkm-apps/x-form
Publish the config (if needed):
php artisan vendor:publish --provider="VkmApps\XForm\XFormServiceProvider"
First Use Case:
Create a Livewire component (php artisan make:livewire UserProfile) and use a basic form component in its Blade view:
<x-form.input
model="user"
name="email"
label="Email Address"
type="email"
/>
Bind the model in the Livewire component:
public $user = [];
public function mount()
{
$this->user = User::find(1)->toArray();
}
Key Files to Review:
/resources/views/vendor/x-form/ (Default Blade components)/config/x-form.php (Configuration options like default classes)/vendor/vkm-apps/x-form/src/ (Core logic for form handling)resources/views/forms/user-profile.blade.php):
<x-form.input model="user" name="name" label="Full Name" />
<x-form.date model="user" name="birthday" range />
<x-form.editor model="user" name="bio" />
@if($showAdvanced)
<x-form.checkbox model="user" name="premium" label="Premium Access" />
@endif
public function rules()
{
return [
'user.email' => 'required|email',
'user.birthday.*' => 'required|date',
];
}
// config/x-form.php
'validation' => [
'custom_rules' => [
'user.premium' => 'accepted_if:user.tier,premium',
],
],
Date Ranges: Use the range attribute for dual-date inputs:
<x-form.date model="user" name="travel_dates" range />
Backend receives: ['travel_dates' => ['2023-01-01', '2023-01-31']].
Rich Text Editors: Integrate with x-form.editor for WYSIWYG:
<x-form.editor model="post" name="content" />
Configure the editor in config/x-form.php:
'editor' => [
'toolbar' => ['bold', 'italic', 'link', 'youtube'],
],
'classes' => [
'input' => 'form-input border-gray-300',
'error' => 'text-red-500 text-sm',
],
x-form.group and x-form.layout for structured forms:
<x-form.layout>
<x-form.group label="Personal Info">
<x-form.input model="user" name="name" />
</x-form.group>
</x-form.layout>
public $dynamicOptions = ['option1', 'option2'];
public function mount()
{
$this->dynamicOptions = ['option3', 'option4'];
}
<x-form.select model="user" name="preference" :options="$dynamicOptions" />
Form Requests: Use Laravel’s FormRequest for centralized validation:
public function rules()
{
return [
'user.*' => ['user.email' => 'required|email'],
];
}
Bind the request to Livewire:
protected $request;
public function mount(Request $request)
{
$this->request = $request;
}
File Uploads: Handle file uploads via Livewire’s $handleUpload or custom logic:
<x-form.file model="post" name="cover_image" />
public function updatedCoverImage($value)
{
$this->validate([
'cover_image' => 'image|max:1024',
]);
}
Partial Updates: Use $refresh to update specific form sections:
public function updateProfile()
{
$this->validate();
// Update user data
$this->refresh();
}
Conditional Logic: Dynamically show/hide fields based on Livewire state:
@if($user->is_premium)
<x-form.input model="user" name="premium_feature" />
@endif
<x-form.input
model="user"
name="email"
x-data="{ showPassword: false }"
x-on:click="$event.target.type = showPassword ? 'password' : 'text'"
/>
XSS Vulnerabilities:
{{!! $message !!}} for error rendering, which can expose XSS risks if error messages include raw user input.$errorMessage = e($this->message); // Use Laravel's e() helper
resources/views/vendor/x-form/error.blade.php:
{!! e($message) !!}
ID Conflicts:
x-form.editor instances on a page may cause ID collisions.id attribute to customize editor IDs:
<x-form.editor model="post" name="content" id="post-content-editor" />
Livewire 3 Migration:
v1 of the package (as noted in v2.0.0 release).composer.json for the correct version constraint:
"vkm-apps/x-form": "^1.0"
Date Range Handling:
public function updatedTravelDates($value)
{
if (is_array($value) && count($value) === 2) {
$this->validate([
'travel_dates.0' => 'required|date',
'travel_dates.1' => 'required|date|after:travel_dates.0',
]);
}
}
Dynamic Arrays:
x-form.editor may not handle dynamic arrays correctly (fixed in v1.1.3).public $user = [
'bio' => '',
'tags' => [],
];
Component Rendering:
{{ dd($this) }} in Livewire components to inspect bound data.php artisan view:clear if components fail to render.Validation Errors:
public function rules()
{
$rules = [
'user.email' => 'required|email',
];
$this->validateOnly($rules);
return $rules;
}
AlpineJS Conflicts:
<div x-data="{}" x-init="console.log('AlpineJS is working')">
<!-- Your form components -->
</div>
Editor Issues:
vkm-js (the editor’s dependency).How can I help you explore Laravel packages today?