Form İşleme ve Validation
Laravel, form gönderimlerini işlemek ve verileri doğrulamak için güçlü araçlar sunar.
Request Nesnesinden Veri Alma
use Illuminate\Http\Request;
public function store(Request $request)
{
// Tek değer
$name = $request->input('name');
$name = $request->name; // Kısayol
// Default değer
$page = $request->input('page', 1);
// Tüm input
$all = $request->all();
$only = $request->only(['name', 'email']);
$except = $request->except(['password']);
// Nested değer
$city = $request->input('address.city');
// Query string
$search = $request->query('q');
// Boolean
$remember = $request->boolean('remember');
// Varlık kontrolü
if ($request->has('name')) { /* ... */ }
if ($request->hasAny(['name', 'email'])) { /* ... */ }
if ($request->filled('name')) { /* boş değilse */ }
if ($request->missing('name')) { /* yoksa */ }
}
Inline Validation
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'required|string|unique:posts,slug',
'content' => 'required|min:100',
'category_id' => 'required|exists:categories,id',
'tags' => 'nullable|array',
'tags.*' => 'exists:tags,id',
'published_at' => 'nullable|date|after:today',
'featured_image' => 'nullable|image|mimes:jpg,png|max:2048',
]);
// Validation başarılıysa $validated array döner
Post::create($validated);
return redirect()->route('posts.index');
}
// Validation başarısızsa otomatik redirect + errors
Validation Kuralları
$rules = [
// String kuralları
'name' => 'required|string|min:2|max:100',
'email' => 'required|email:rfc,dns',
'url' => 'required|url',
'alpha' => 'alpha', // Sadece harf
'alpha_num' => 'alpha_num', // Harf ve rakam
'alpha_dash' => 'alpha_dash', // Harf, rakam, tire, alt çizgi
// Sayısal kuralları
'age' => 'required|integer|min:18|max:100',
'price' => 'required|numeric|between:0,99999.99',
'quantity' => 'required|digits:4',
'code' => 'required|digits_between:4,8',
// Tarih kuralları
'birth_date' => 'required|date|before:today',
'start_date' => 'required|date|after:today',
'end_date' => 'required|date|after:start_date',
'event_date' => 'required|date_format:Y-m-d H:i',
// Dosya kuralları
'avatar' => 'nullable|image|mimes:jpg,png,gif|max:2048',
'document' => 'required|file|mimetypes:application/pdf|max:10240',
'photos' => 'required|array|min:1|max:5',
'photos.*' => 'image|max:2048',
// Veritabanı kuralları
'email' => 'required|unique:users,email',
'email' => 'required|unique:users,email,' . $user->id, // Güncelleme
'category_id' => 'required|exists:categories,id',
// Koşullu kurallar
'password' => 'required|confirmed', // password_confirmation alanı gerekli
'country' => 'required_if:has_address,true',
'state' => 'required_with:country',
'nickname' => 'required_without:name',
// Özel kurallar
'terms' => 'accepted',
'ip' => 'ip',
'mac' => 'mac_address',
'uuid' => 'uuid',
'json' => 'json',
'timezone' => 'timezone',
];
Form Request
php artisan make:request StorePostRequest
check()
}
/**
* Validation kuralları
*/
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'slug' => ['required', 'string', 'unique:posts,slug'],
'content' => ['required', 'string', 'min:100'],
'category_id' => ['required', 'exists:categories,id'],
'tags' => ['nullable', 'array'],
'tags.*' => ['exists:tags,id'],
];
}
/**
* Özel hata mesajları
*/
public function messages(): array
{
return [
'title.required' => 'Başlık alanı zorunludur.',
'title.max' => 'Başlık en fazla :max karakter olabilir.',
'slug.unique' => 'Bu URL adresi zaten kullanılıyor.',
'content.min' => 'İçerik en az :min karakter olmalıdır.',
];
}
/**
* Attribute isimleri
*/
public function attributes(): array
{
return [
'title' => 'başlık',
'content' => 'içerik',
'category_id' => 'kategori',
];
}
/**
* Validation sonrası veri hazırlama
*/
protected function prepareForValidation(): void
{
$this->merge([
'slug' => Str::slug($this->title),
'user_id' => auth()->id(),
]);
}
}
// Controller'da kullanım
public function store(StorePostRequest $request)
{
// Validation otomatik yapılır
$post = Post::create($request->validated());
return redirect()->route('posts.show', $post);
}
Blade'de Hata Gösterimi
Önemli Noktalar
- Request sınıfı ile form verilerine erişilir
- validate() metodu ile inline validation
- Form Request ile validation logic'i ayrıştırılır
- @error direktifi ile hata mesajları gösterilir
- old() helper ile form değerleri korunur