laravel-form

Versie
7.3.1
Downloads
8.608 downloads
Licentie
MIT
GitHub

Troubleshooting

Common Issues

Q: Why isn't my form field populating with the model value?

A: Check the value resolution priority order:

  1. Is there old input in the session? (After validation failure, old input takes precedence)
  2. Is considerRequest() enabled and is there request data?
  3. Did you pass an explicit value parameter?
  4. Is the model actually set? Use Form::getModel() to verify
  5. Is the attribute name correct and accessible on the model?
// Debug value resolution
@if(Form::getModel())
    Model is set: {{ get_class(Form::getModel()) }}
@endif

// Check what value the form builder is using
{{ Form::getValueAttribute('field_name') }}

Q: How do I access nested model relationships in forms?

A: Use dot notation:

{{ Form::model($user, ['route' => 'profile.update']) }}
    {{-- Access nested relationships --}}
    {{ Form::text('profile.bio') }}
    {{ Form::text('company.address.city') }}
{{ Form::close() }}

Make sure the relationships are loaded (use $user->load('profile', 'company.address')).

Q: My form mutators aren't working

A: Verify the following:

  1. You've added the FormAccessible trait to your model
  2. The method name follows the pattern: form{AttributeName}Attribute
  3. The attribute name matches exactly (case-sensitive)
  4. You're using Form::model() for model binding
// Correct mutator pattern
public function formBirthDateAttribute($value)
{
    return $value ? $value->format('Y-m-d') : null;
}

Q: Can I disable CSRF protection for a specific form?

A: CSRF protection is automatic. If you need to disable it (not recommended), you can:

  1. Remove the token manually (not ideal): Don't use Form::open(), write raw HTML
  2. Exclude the route in VerifyCsrfToken middleware
  3. Add the route to $except array in the CSRF middleware

Better approach: Keep CSRF enabled and handle it properly in your JavaScript/AJAX calls.

Q: Old input isn't showing after validation failure

A: This usually means the session isn't configured properly:

  1. Verify session configuration in config/session.php
  2. Check that the session middleware is active for your route
  3. Ensure you're redirecting with withInput(): return back()->withInput()
  4. Verify the FormBuilder has access to the session (dependency injection handles this automatically)

Q: How do I create custom form components?

A: Register them in a service provider:

// In AppServiceProvider or custom provider
public function boot()
{
    Form::component('bsText', 'components.form.bootstrap-text', ['name', 'value', 'attributes']);
}

See the Custom Components section for full examples.

Q: Date inputs aren't formatting correctly

A: DateTime objects are automatically formatted for HTML5 date inputs. If using custom formats:

// The form builder handles these automatically:
{{ Form::date('date', $model->date) }}  // Expects Y-m-d
{{ Form::datetime('datetime', $model->datetime) }}  // RFC3339 format
{{ Form::datetimeLocal('datetime', $model->datetime) }}  // Y-m-d\TH:i

// If you need custom formatting, use a form mutator:
public function formCustomDateAttribute($value)
{
    return $value ? $value->format('m/d/Y') : null;
}

Q: How do I add custom validation display?

A: Combine with Laravel's error handling:

{{ Form::text('email', null, ['class' => 'form-control ' . ($errors->has('email') ? 'is-invalid' : '')]) }}

@error('email')
    <div class="invalid-feedback">{{ $message }}</div>
@enderror

Q: Can I use this with Vue/React components?

A: Yes, but you have two approaches:

  1. Server-side rendering: Use Form Builder in Blade, then hydrate with JavaScript
  2. API approach: Build forms in JavaScript, use Laravel for API endpoints only

For SPAs, consider using the Form Builder primarily for initial page loads or progressive enhancement.

Een fout gevonden of een vraag? Maak een issue aan op GitHub.