默认 Laravel Breeze 返回有关必填字段的错误

问题描述 投票:0回答:1

我正在刷新我的 Laravel 技能,因为我已经使用它两年了。我想说有些事情已经改变了,而我正在努力解决其中的一些问题。

我想注册用户并查看 Laravel Breeze 教程,只需使用 Breeze 自带的默认设置就可以完成。

但是,我无法注册新用户,因为我收到所有字段的错误消息,告诉我该字段是必填字段。好吧,我可以向你保证,我确实用正确的数据填写了表格,但它仍然会给我错误,而且似乎我是唯一遇到这个问题的人..

我的刀片文件:

<x-guest-layout>
    <form method="POST" action="{{ route('register') }}">
        @csrf

        <!-- Name -->
        <div>
            <x-input-label for="name" :value="__('Name')"  class="text-white" />
            <x-text-input id="name" class="block mt-1 w-full bg-gray-100 border-b-0" type="text" name="name" :value="old('name')" required autofocus autocomplete="name" />
            <x-input-error :messages="$errors->get('name')" class="mt-2" />
        </div>

        <!-- Username | This will be visible to the public -->
        <div class="mt-4">
            <x-input-label for="username" :value="__('Username')"  class="text-white" />
            <x-text-input id="username" class="block mt-1 w-full bg-gray-100 border-b-0" type="text" name="username" :value="old('username')" required autofocus autocomplete="username" />
            <x-input-error :messages="$errors->get('username')" class="mt-2" />
        </div>

        <!-- Email Address -->
        <div class="mt-4">
            <x-input-label for="email" :value="__('Email')" class="text-white" />
            <x-text-input id="email" class="block mt-1 w-full bg-gray-100 border-b-0" type="email" name="email" :value="old('email')" required autocomplete="username" />
            <x-input-error :messages="$errors->get('email')" class="mt-2" />
        </div>

        <!-- Password -->
        <div class="mt-4">
            <x-input-label for="password" :value="__('Password')" class="text-white" />

            <x-text-input id="password" class="block mt-1 w-full border-b-0"
                            type="password"
                            name="password"
                            required 
                            autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password')" class="mt-2" />
        </div>

        <!-- Confirm Password -->
        <div class="mt-4">
            <x-input-label for="password_confirmation" :value="__('Confirm Password')" class="text-white" />

            <x-text-input id="password_confirmation" class="block mt-1 w-full border-b-0"
                            type="password"
                            name="password_confirmation" 
                            required 
                            autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password_confirmation')" class="mt-2" />
        </div>

        <div class="flex items-center justify-end mt-4">
            <a class="underline text-sm text-gray-600 dark:text-white hover:text-gray-900 dark:hover:text-gray-100 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-800" href="{{ route('login') }}">
                {{ __('Already registered?') }}
            </a>

            <x-primary-button class="ms-4">
                {{ __('Register') }}
            </x-primary-button>
        </div>
    </form>
</x-guest-layout>

我的注册AuthController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\View\View;

class RegisteredUserController extends Controller
{
    /**
     * Display the registration view.
     */
    public function create(): View
    {
        return view('auth.register');
    }

    /**
     * Handle an incoming registration request.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request): RedirectResponse
    {
        
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'username' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);

        dd('halllooooooo');

        $user = User::create([
            'name' => $request->name,
            'username' => $request->username,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        dd($user);

        event(new Registered($user));

        Auth::login($user);

        return redirect(route('dashboard', absolute: false));
    }
}

用户模型

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'username',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }
}

如果您想看到我重现这些步骤,您可以观看这台织机: https://www.loom.com/share/d6bf15d9d3ff46929cbad9ba41fb2019?sid=5515dcee-80db-4fff-9613-cdfb446bfbc9

php laravel breeze
1个回答
0
投票

对于遇到此问题的其他人:

在 Laravel 中更新视图时,缓存的视图通常会导致浏览器无法渲染代码中的实际内容。

在这种情况下,请检查浏览器中的 DOM,查看每个表单输入上的“name”属性是否已正确呈现。

如果没有,请使用

清除视图缓存
php artisan view:clear

然后重新加载页面并检查表单输入。您现在应该看到正确呈现的名称,并且表单输入应传递到控制器

© www.soinside.com 2019 - 2024. All rights reserved.