I'm just learning Livewire and have been following some screencasts doing some basic form handling. My component view that has the form looks like this:
<form wire:submit.prevent="register" class="space-y-6" action="#" method="POST">
<div>
<label for="email" class="block text-sm font-medium text-gray-700"> Email address </label>
<div class="mt-1">
<input wire:model="email" id="email" name="email" type="email" autocomplete="email" required class="@error('email') border-red-500 @enderror appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
@error('email') <div class="mt-1 text-red-500 text-sm">{{ $message }}</div> @enderror
</div>
<div>
<button type="submit" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">Register</button>
</div>
</form>
This component is trying to use the register function looks like this:
class Register extends Component
{
public string $email = '';
public string $password = '';
public string $passwordConfirmation = '';
public function updatedEmail() {
$this->validate(['email' => 'unique:users']);
}
public function register(): Redirector|Application|RedirectResponse
{
$data = $this->validate([
'email' => 'required|email|unique:users',
'password' => 'required|min:6|max:20|same:passwordConfirmation',
'passwordConfirmation' => 'required'
]);
$user = User::create([
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
auth()->login($user);
return redirect('/');
}
public function render()
{
return view('livewire.auth.register');
}
}
Finally I'm using the normal app.blade.php layout file in the default location, and my web.php routes look like this:
Route::get('/', function () {
return view('welcome');
});
Route::get('/register', Register::class);
According to Livewire docs (https://laravel-livewire.com/docs/2.x/rendering-components#page-components) for full page components, I'm passing everything through correctly to the slot in the app layout. So just in case here is the layout view as well (in resources/views/layouts/app.blade.php):
<html>
<head>
@vite('resources/css/app.css')
@livewireStyles
</head>
<body>
{{ $slot }}
@livewireScripts
</body>
</html>
I feel like I must be missing something really dumb or simple here that would explain why the form submit prevention isn't working and actually ending up doing the POST. Anyone have any pointers or been through this?
Thank you!