Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

X Form Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require vkm-apps/x-form
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="VkmApps\XForm\XFormServiceProvider"
    
  2. 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();
    }
    
  3. 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)

Implementation Patterns

Core Workflows

1. Form Definition

  • Reusable Form Components: Define form fields in a dedicated Blade file (e.g., 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" />
    
  • Dynamic Forms: Use Livewire properties to conditionally render fields:
    @if($showAdvanced)
        <x-form.checkbox model="user" name="premium" label="Premium Access" />
    @endif
    

2. Validation and Submission

  • Automatic Validation: Leverage Livewire’s built-in validation:
    public function rules()
    {
        return [
            'user.email' => 'required|email',
            'user.birthday.*' => 'required|date',
        ];
    }
    
  • Custom Validation: Extend the package’s validation logic via config or custom components:
    // config/x-form.php
    'validation' => [
        'custom_rules' => [
            'user.premium' => 'accepted_if:user.tier,premium',
        ],
    ],
    

3. Complex Fields

  • 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'],
    ],
    

4. Layout and Styling

  • Consistent Styling: Override default classes in the config:
    'classes' => [
        'input' => 'form-input border-gray-300',
        'error' => 'text-red-500 text-sm',
    ],
    
  • Form Layouts: Use 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>
    

5. Dynamic Data

  • Livewire Properties: Bind dynamic data to form fields:
    public $dynamicOptions = ['option1', 'option2'];
    
    public function mount()
    {
        $this->dynamicOptions = ['option3', 'option4'];
    }
    
    <x-form.select model="user" name="preference" :options="$dynamicOptions" />
    

Integration Tips

Laravel Ecosystem

  • 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',
        ]);
    }
    

Livewire-Specific

  • 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
    

AlpineJS Enhancements

  • Client-Side Interactivity: Extend components with AlpineJS:
    <x-form.input
        model="user"
        name="email"
        x-data="{ showPassword: false }"
        x-on:click="$event.target.type = showPassword ? 'password' : 'text'"
    />
    

Gotchas and Tips

Pitfalls

  1. XSS Vulnerabilities:

    • Issue: The package uses {{!! $message !!}} for error rendering, which can expose XSS risks if error messages include raw user input.
    • Fix: Sanitize error messages before passing them to the view:
      $errorMessage = e($this->message); // Use Laravel's e() helper
      
    • Workaround: Override the error component in resources/views/vendor/x-form/error.blade.php:
      {!! e($message) !!}
      
  2. ID Conflicts:

    • Issue: Multiple x-form.editor instances on a page may cause ID collisions.
    • Fix: Use the id attribute to customize editor IDs:
      <x-form.editor model="post" name="content" id="post-content-editor" />
      
  3. Livewire 3 Migration:

    • Issue: If using Livewire 3, ensure compatibility by using v1 of the package (as noted in v2.0.0 release).
    • Fix: Check composer.json for the correct version constraint:
      "vkm-apps/x-form": "^1.0"
      
  4. Date Range Handling:

    • Issue: Backend expects an array for date ranges, which may not align with all validation rules.
    • Fix: Normalize the input in the Livewire component:
      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',
              ]);
          }
      }
      
  5. Dynamic Arrays:

    • Issue: The x-form.editor may not handle dynamic arrays correctly (fixed in v1.1.3).
    • Fix: Ensure the model property is an array:
      public $user = [
          'bio' => '',
          'tags' => [],
      ];
      

Debugging Tips

  1. Component Rendering:

    • Use {{ dd($this) }} in Livewire components to inspect bound data.
    • Check Blade errors with php artisan view:clear if components fail to render.
  2. Validation Errors:

    • Log validation errors for debugging:
      public function rules()
      {
          $rules = [
              'user.email' => 'required|email',
          ];
          $this->validateOnly($rules);
          return $rules;
      }
      
  3. AlpineJS Conflicts:

    • Disable AlpineJS temporarily to isolate issues:
      <div x-data="{}" x-init="console.log('AlpineJS is working')">
          <!-- Your form components -->
      </div>
      
  4. Editor Issues:

    • Clear browser cache or use incognito mode if editors fail to load.
    • Check the browser console for errors related to vkm-js (the editor’s dependency).

Extension Points

  1. Custom Components:
    • Extend the
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views