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

Collective Input Laravel Package

christhompsontldr/collective-input

Laravel helper package for working with request input arrays using a fluent, Collection-style API. Simplifies nested input access, filtering, default values, and transformations so you can validate, sanitize, and map form data with less boilerplate.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require christhompsontldr/collective-input
    

    Ensure laravelcollective/html (v6.x) is installed as a dependency.

  2. Publish Config (Optional):

    php artisan vendor:publish --provider="Collective\Html\HtmlServiceProvider" --tag=config
    

    Modify config/html.php if needed (e.g., default attributes, Bootstrap version).

  3. First Use Case: Replace vanilla Blade form helpers with Collective’s Bootstrap-aware versions:

    {!! Form::open(['class' => 'form-horizontal']) !!}
      {!! Form::text('email', null, ['class' => 'form-control', 'placeholder' => 'Email']) !!}
      {!! Form::submit('Submit', ['class' => 'btn btn-primary']) !!}
    {!! Form::close() !!}
    
    • Key: Wrap inputs in form-horizontal (Bootstrap 4) or form-group for alignment.

Implementation Patterns

Core Workflows

  1. Form Layouts:

    • Use Form::open() with class="form-horizontal" for stacked labels/inputs.
    • Group inputs with Form::label() + Form::control() (alias for Form::input() with class="form-control"):
      <div class="form-group">
        {!! Form::label('name', 'Full Name', ['class' => 'control-label']) !!}
        {!! Form::control('name', null, ['placeholder' => 'John Doe']) !!}
      </div>
      
  2. Dynamic Attributes:

    • Merge classes/attributes dynamically:
      {!! Form::select('role', $roles, null, [
          'class' => 'form-control' . ($errors->has('role') ? ' is-invalid' : ''),
          'data-toggle' => 'selectpicker'
      ]) !!}
      
  3. Validation Integration:

    • Leverage Laravel’s validation + Collective’s error display:
      @if($errors->has('email'))
        <div class="invalid-feedback">{{ $errors->first('email') }}</div>
      @endif
      
  4. Bootstrap Components:

    • Pre-built helpers for checkboxes/radio buttons:
      {!! Form::checkbox('subscribe', 1, old('subscribe', true), ['class' => 'form-check-input']) !!}
      {!! Form::label('subscribe', 'Subscribe', ['class' => 'form-check-label']) !!}
      
  5. Asset Integration:

    • Use Form::asset() for CSRF tokens, method spoofing, or hidden fields:
      {!! Form::asset('meta', '<meta name="referrer" content="no-referrer">') !!}
      

Advanced Patterns

  • Custom Field Types: Extend the package by creating a custom field class (see collective/html/src/FormBuilder):

    // app/Extensions/CustomField.php
    use Collective\Html\FormBuilder;
    
    class CustomField extends FormBuilder {
        public static function customInput($name, $value = null, $options = []) {
            return '<input type="custom" name="' . e($name) . '" value="' . e($value) . '" ' . e($options) . '>';
        }
    }
    

    Register in AppServiceProvider@boot():

    FormBuilder::macro('customInput', function($name, $value = null, $options = []) {
        return CustomField::customInput($name, $value, $options);
    });
    
  • Form Macros: Create reusable form sections:

    // app/Providers/AppServiceProvider.php
    use Illuminate\Support\Facades\Form;
    
    public function boot() {
        Form::macro('addressFields', function($prefix) {
            return <<<HTML
            <div class="form-group">
                {!! Form::label($prefix . '[street]', 'Street') !!}
                {!! Form::text($prefix . '[street]', null, ['class' => 'form-control']) !!}
            </div>
            HTML;
        });
    }
    

    Usage:

    {!! Form::addressFields('user') !!}
    

Gotchas and Tips

Common Pitfalls

  1. Bootstrap Version Mismatch:

    • The package assumes Bootstrap 4. Use class="form-control" (not form-control in v3).
    • Fix: Override config or manually adjust classes.
  2. CSRF Token Conflicts:

    • If using Form::open() without route() or url(), ensure @csrf is included separately:
      {!! Form::open(['class' => 'form-horizontal']) !!}
      @csrf
      
  3. Macro Overrides:

    • Collective’s macros (e.g., Form::control()) may conflict with custom macros.
    • Tip: Use Form::macro() in AppServiceProvider@boot() to extend after Collective loads.
  4. Asset Helpers:

    • Form::asset() is not for form fields—use it for non-input HTML (e.g., <meta>, <script>).
  5. Deprecated Methods:

    • Avoid Form::model() (Laravel 5.x). Use Form::open(['model' => $model]) instead.

Debugging Tips

  • Inspect Output: Use {!! Form::text('field')->render() !!} to debug raw HTML.
  • Check Config: Verify config/html.php for default attributes (e.g., default_options).
  • Clear Views: Run php artisan view:clear if macros stop working.

Performance Quirks

  • Avoid Nested Macros: Deeply nested Form::macro() calls can bloat Blade templates.
  • Cache Forms: For complex forms, cache the Blade output:
    // app/Providers/AppServiceProvider.php
    public function boot() {
        Blade::directive('cacheForm', function($expr) {
            return "<?php echo \$__env->make('{$expr}')->render(); ?>";
        });
    }
    
    Usage:
    @cacheForm('forms.partial')
    

Extension Points

  1. Custom Validators: Extend FormBuilder to add validation-aware fields:

    Form::macro('phone', function($name, $value = null, $options = []) {
        return Form::text($name, $value, array_merge($options, [
            'class' => 'form-control' . ($errors->has($name) ? ' is-invalid' : '')
        ]));
    });
    
  2. Localization: Override labels/placeholders via language files (resources/lang/en/form.php):

    return [
        'labels' => [
            'email' => 'Email Address',
        ],
    ];
    

    Access in Blade:

    {!! Form::label('email', trans('form.labels.email')) !!}
    
  3. Event Listeners: Hook into form submission via Laravel events (e.g., FormSubmitting):

    // app/Providers/EventServiceProvider.php
    protected $listen = [
        'Collective\Html\FormEvents\FormSubmitting' => [
            'App\Listeners\LogFormSubmission',
        ],
    ];
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport