Don ingantawa da sarrafa bayanan shigar da bayanai daga fom ta amfani da validation fasalin a cikin Laravel, bi waɗannan matakan:
Ƙayyadaddun Validation Dokoki
Fara da ayyana validation ƙa'idodin filayen fom ɗin ku. Laravel yana ba da dokoki daban-daban validation waɗanda za ku iya amfani da su don tabbatar da daidaito da ingancin bayanan.
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:users|max:255',
'password' => 'required|min:8',
]);
// Process the validated data
$user = User::create([
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'password' => Hash::make($validatedData['password']),
]);
// Redirect to a success page or perform other actions
return redirect()->route('users.index')->with('success', 'User created successfully.');
}
A cikin misalin da ke sama, muna ayyana validation dokoki don filayen suna, imel, da kalmar sirri. Dokar required
tana tabbatar da cewa filayen ba su da komai, email
ƙa'idar ta tabbatar da tsarin imel, unique:users
ƙa'idar tana bincika idan imel ɗin ya kasance na musamman a cikin users
tebur, kuma ka'idoji max
da min
ƙa'idodi sun ƙayyade matsakaicin matsakaicin matsakaicin matsakaicin filin kalmar sirri.
Karɓar Validation Sakamako
Laravel Siffar ta validation ta atomatik tana aiwatar validation da bisa ƙayyadaddun ƙa'idodin. Idan validation abin ya gaza, Laravel zai sake tura mai amfani zuwa fom tare da saƙon kuskure masu dacewa. Kuna iya dawo da waɗannan saƙonnin kuskure a cikin ra'ayin ku don nuna su ga mai amfani.
<!-- Display validation errors -->
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Create user form -->
<form method="POST" action="{{ route('users.store') }}">
@csrf
<input type="text" name="name" placeholder="Name" value="{{ old('name') }}">
<input type="email" name="email" placeholder="Email" value="{{ old('email') }}">
<input type="password" name="password" placeholder="Password">
<button type="submit">Create User</button>
</form>
A cikin lambar da ke sama, muna bincika idan akwai wasu validation kurakurai kuma mu nuna su a cikin akwatin faɗakarwa. Ana amfani da aikin old()
don sake cika filayen fom tare da ƙimar da aka shigar a baya idan an sami validation kuskure.
Ta bin wannan misalin, zaku iya ingantawa da sarrafa bayanan shigarwa daga sifofi ta amfani da validation fasalin a Laravel. Wannan yana tabbatar da cewa bayanan sun cika ƙayyadaddun ƙa'idodin ku kuma suna taimakawa kiyaye amincin bayanai a cikin aikace-aikacen ku.