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.
Installation:
composer require christhompsontldr/collective-input
Ensure laravelcollective/html (v6.x) is installed as a dependency.
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).
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() !!}
form-horizontal (Bootstrap 4) or form-group for alignment.Form Layouts:
Form::open() with class="form-horizontal" for stacked labels/inputs.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>
Dynamic Attributes:
{!! Form::select('role', $roles, null, [
'class' => 'form-control' . ($errors->has('role') ? ' is-invalid' : ''),
'data-toggle' => 'selectpicker'
]) !!}
Validation Integration:
@if($errors->has('email'))
<div class="invalid-feedback">{{ $errors->first('email') }}</div>
@endif
Bootstrap Components:
{!! Form::checkbox('subscribe', 1, old('subscribe', true), ['class' => 'form-check-input']) !!}
{!! Form::label('subscribe', 'Subscribe', ['class' => 'form-check-label']) !!}
Asset Integration:
Form::asset() for CSRF tokens, method spoofing, or hidden fields:
{!! Form::asset('meta', '<meta name="referrer" content="no-referrer">') !!}
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') !!}
Bootstrap Version Mismatch:
class="form-control" (not form-control in v3).CSRF Token Conflicts:
Form::open() without route() or url(), ensure @csrf is included separately:
{!! Form::open(['class' => 'form-horizontal']) !!}
@csrf
Macro Overrides:
Form::control()) may conflict with custom macros.Form::macro() in AppServiceProvider@boot() to extend after Collective loads.Asset Helpers:
Form::asset() is not for form fields—use it for non-input HTML (e.g., <meta>, <script>).Deprecated Methods:
Form::model() (Laravel 5.x). Use Form::open(['model' => $model]) instead.{!! Form::text('field')->render() !!} to debug raw HTML.config/html.php for default attributes (e.g., default_options).php artisan view:clear if macros stop working.Form::macro() calls can bloat Blade templates.// app/Providers/AppServiceProvider.php
public function boot() {
Blade::directive('cacheForm', function($expr) {
return "<?php echo \$__env->make('{$expr}')->render(); ?>";
});
}
Usage:
@cacheForm('forms.partial')
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' : '')
]));
});
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')) !!}
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',
],
];
How can I help you explore Laravel packages today?