Alexandru019 wrote a comment+100 XP
1mo ago
@radmax @andrewdv8 @halvarado77
Solution for Browser Test Issues with File Upload Forms
I experienced the same issue and found a solution based on a comment from the next video. The problem occurs when using enctype="multipart/form-data" on forms with Playwright browser tests.
The Fix
The solution is to dynamically set the enctype attribute using Alpine.js, only when a file is actually selected.
Step 1: Update the form's x-data
In resources/views/idea/index.blade.php, add a new hasImage property to track when a file is selected:
<form
x-data="{
status : 'pending',
newLink: '',
links: [],
newStep: '',
steps: [],
hasImage: false
}"
action="{{ route('idea.store') }}"
method="POST"
class="space-y-4"
x-bind:enctype="hasImage ? 'multipart/form-data' : false"
>
Step 2: Replace static enctype with dynamic binding
Replace the static enctype="multipart/form-data" attribute with:
x-bind:enctype="hasImage ? 'multipart/form-data' : false"
Step 3: Update the file input
Add an @change event to the file input to toggle hasImage when a file is selected:
<input
type="file"
name="image"
accept="image/*"
@change="hasImage = $event.target.files.length > 0"
/>
Why This Works
This approach prevents the form from using multipart/form-data encoding when no image is uploaded, which resolves conflicts with Playwright browser tests. The enctype is only set when an actual file is selected by the user.
Benefits
- ✅ Browser tests pass successfully without modifications to Composer packages
- ✅ File uploads still work correctly for end users
- ✅ No impact on form functionality
- ✅ Clean, maintainable solution using Alpine.js
Test Results
After implementing these changes:
PASS Tests\Browser\CreateIdeaTest
✓ it creates a new idea (1.95s)
Tests: 1 passed (9 assertions)
Duration: 2.42s
Hope this helps!
Alexandru019 wrote a comment+100 XP
1mo ago
@thesnakebite thank you! You literally saved me <3
Alexandru019 liked a comment+100 XP
1mo ago
Thanks @marc_meat! 🙌🏻 After investigating, I confirmed that this is indeed the error. I found a possible workaround without modifying the Composer fork: dynamically setting the enctype with Alpine.js only when the user selects an image:
x-bind:enctype="hasImage ? 'multipart/form-data' : false"
This way, the form is submitted without multipart/form-data by default, and the browser test passes, but when the user selects an image, it changes automatically. I hope this helps!