我正在尝试使用Laravel进行第一次表单验证,由于某种原因,第一个字段(全名(不接受我键入的任何内容,并返回错误"全名字段是必需的";。
我的表单视图
<!-- CONTACT -->
<section>
<div>
<h3>Contact me for more information</h3>
@if (session('status'))
<p>{{ session('status') }}</p>
@endif
<form action="/contact" method="POST">
{{ csrf_field() }}
<div>
<div class="relative">
<label for="text">Full Name</label>
<input type="text" name="fullname" placeholder="John Doe" value="{{ old('fullName') }}" required />
@error('fullName')
<div>{{ $message }}</div>
@enderror
</div>
<div class="relative">
<label for="email">E-mail</label>
<input type="email" name="email" placeholder="email@example.com" value="{{ old('email') }}" required />
@error('email')
<div>{{ $message }}</div>
@enderror
</div>
</div>
<div class="relative">
<label for="text">Phone Number</label>
<input type="text" name="phoneNumber" placeholder="(123) 456-7890" value="{{ old('phoneNumber') }}" />
@error('phoneNumber')
<div>{{ $message }}</div>
@enderror
</div>
<div class="relative">
<label for="textarea">Message</label>
@error('message')
<div>{{ $message }}</div>
@enderror
<textarea type="textarea" name="message" rows="6" placeholder="Message" value="{{ old('message') }}" required></textarea>
</div>
<button type="submit">Send</button>
</form>
</div>
</section>
ContactController
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'fullName' => 'required|string|max:70',
'email' => 'required|email|max:255',
'phoneNumber' => 'nullable|regex:^(+d{1,2}s)?(?d{3})?[s.-]d{3}[s.-]d{4}$^',
'message' => 'required|string|min:50',
]);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
$contact = new Contact;
$contact->fullName = $request->fullName;
$contact->email = $request->email;
$contact->phoneNumber = $request->phoneNumber;
$contact->message = $request->message;
$contact->save();
return redirect('/contact')->with('status', 'Thanks for contacting me. I'll be in touch soon!');
}
联系方式
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Contact extends Model
{
use HasFactory;
protected $table = 'contacts';
protected $fillable = ['fullName', 'email', 'phoneNumber', 'message'];
}
迁移文件
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('fullName');
$table->string('email');
$table->string('phoneNumber')->nullable;
$table->text('message');
$table->timestamps();
});
}
为什么第一个字段";fullName";什么都不接受?无论我输入什么,它都会显示必填字段为空。谢谢
您的名称字段有一个拼写错误,它是fullName而不是fullName
<div class="relative">
<label for="text">Full Name</label>
<input type="text" name="fullName" placeholder="John Doe" value="{{ old('fullName') }}" required />
@error('fullName')
<div>{{ $message }}</div>
@enderror
</div>
<div>
<h3>Contact me for more information</h3>
@if (session('status'))
<p>{{ session('status') }}</p>
@endif
<form action="/contact" method="POST">
{{ csrf_field() }}
<div>
<div class="relative">
<label for="text">Full Name</label>
<input type="text" name="fullName" placeholder="John Doe" value="{{ old('fullName') }}" required />
@error('fullName')
<div>{{ $message }}</div>
@enderror
</div>
<div class="relative">
<label for="email">E-mail</label>
<input type="email" name="email" placeholder="email@example.com" value="{{ old('email') }}" required />
@error('email')
<div>{{ $message }}</div>
@enderror
</div>
</div>
<div class="relative">
<label for="text">Phone Number</label>
<input type="text" name="phoneNumber" placeholder="(123) 456-7890" value="{{ old('phoneNumber') }}" />
@error('phoneNumber')
<div>{{ $message }}</div>
@enderror
</div>
<div class="relative">
<label for="textarea">Message</label>
@error('message')
<div>{{ $message }}</div>
@enderror
<textarea type="textarea" name="message" rows="6" placeholder="Message" value="{{ old('message') }}" required></textarea>
</div>
<button type="submit">Send</button>
</form>
</div>
</section>
请确保您注意变量名称Laravel是区分大小写的。
全名出现错误
我建议你使用full_name,mobile_number之类的snake case。