Laravel 验证 - 全名字段为必填项
Laravel Validation - The full name field is required
我正在尝试使用 Laravel 进行第一次表单验证,由于某些原因第一个字段(全名)不接受我键入的任何内容并且 return 出现错误“全名字段是必需的。
我的表单视图。
<!-- 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>
联系人控制器
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
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”不接受任何内容吗?无论我输入什么,它都说必填字段为空。谢谢。
您的姓名字段有错字,是全名而不是 全名
<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 蛇形盒。
我正在尝试使用 Laravel 进行第一次表单验证,由于某些原因第一个字段(全名)不接受我键入的任何内容并且 return 出现错误“全名字段是必需的。
我的表单视图。
<!-- 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>
联系人控制器
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
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”不接受任何内容吗?无论我输入什么,它都说必填字段为空。谢谢。
您的姓名字段有错字,是全名而不是 全名
<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 蛇形盒。