Skip to content

Commit 2106866

Browse files
committed
Validation
1 parent 26e5e32 commit 2106866

File tree

8 files changed

+98
-8
lines changed

8 files changed

+98
-8
lines changed

app/Http/Controllers/User/CheckoutController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\Checkout;
77
use Illuminate\Http\Request;
88
use App\Http\Controllers\Controller;
9+
use App\Http\Requests\User\Checkout\Store;
910
use Auth;
1011

1112
class CheckoutController extends Controller
@@ -25,8 +26,12 @@ public function index()
2526
*
2627
* @return \Illuminate\Http\Response
2728
*/
28-
public function create(Camp $camp)
29+
public function create(Request $request, Camp $camp)
2930
{
31+
if($camp->isRegistered){
32+
$request->session()->flash('error', "You already registered on {$camp->title}!");
33+
return redirect(route('dashboard'));
34+
}
3035
return view('checkout.create',[
3136
'camp' => $camp
3237
]);
@@ -37,8 +42,10 @@ public function create(Camp $camp)
3742
*
3843
* @param \Illuminate\Http\Request $request
3944
* @return \Illuminate\Http\Response
45+
*
46+
* Store $request karena masuk ke Request/User/Checkout/Store dulu
4047
*/
41-
public function store(Request $request, Camp $camp)
48+
public function store(Store $request, Camp $camp)
4249
{
4350
// mapping request data
4451
$data = $request->all();

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function handleProviderCallback()
3434
'email' => $data['email']
3535
], $data);
3636
Auth::login($user, true);
37+
3738
return redirect(route('welcome'));
3839
}
3940
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Requests\User\Checkout;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Auth;
7+
8+
class Store extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return Auth::check();
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
$expiredValidation = date('Y-m', time());
28+
return [
29+
'name' => 'required|string',
30+
'email' => 'required|email|unique:users,email,'.Auth::id().',id',
31+
'occupation' => 'required|string',
32+
'card_number' => 'required|numeric|digits_between:8,16',
33+
'expired' => 'required|date|date_format:Y-m|after_or_equal:'.$expiredValidation,
34+
'cvc' => 'required|numeric|digits:3'
35+
];
36+
}
37+
}

app/Models/Camp.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\SoftDeletes;
8+
use App\Models\Checkout;
9+
use Auth;
810

911
class Camp extends Model
1012
{
1113
use HasFactory, SoftDeletes;
1214

1315
protected $fillable = ['title', 'price'];
16+
17+
public function getIsRegisteredAttribute()
18+
{
19+
if(!Auth::check()){
20+
return false;
21+
}
22+
23+
return Checkout::whereCampId($this->id)->whereUserId(Auth::id())->exists();
24+
}
25+
1426
}

resources/views/checkout/create.blade.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,47 @@
3838
@csrf
3939
<div class="mb-4">
4040
<label class="form-label">Full Name</label>
41-
<input name="name" type="text" class="form-control" value="{{Auth::user()->name}}">
41+
<input name="name" type="text" class="form-control {{$errors->has('name') ? 'is-invalid' : '' }}" value="{{Auth::user()->name}}">
42+
@if ($errors->has('name'))
43+
<p class="text-danger">{{$errors->first('name')}}</p>
44+
@endif
4245
</div>
4346
<div class="mb-4">
4447
<label class="form-label">Email Address</label>
45-
<input name="email" type="email" class="form-control" value="{{Auth::user()->email}}">
48+
<input name="email" type="email" class="form-control {{$errors->has('email') ? 'is-invalid' : '' }}" value="{{Auth::user()->email}}">
49+
@if ($errors->has('email'))
50+
<p class="text-danger">{{$errors->first('email')}}</p>
51+
@endif
4652
</div>
4753
<div class="mb-4">
4854
<label class="form-label">Occupation</label>
49-
<input name="occupation" type="text" class="form-control" value="{{Auth::user()->occupation}}">
55+
<input name="occupation" type="text" class="form-control {{$errors->has('occupation') ? 'is-invalid' : '' }}" value="{{old('occupation') ? : Auth::user()->occupation}}">
56+
@if ($errors->has('occupation'))
57+
<p class="text-danger">{{$errors->first('occupation')}}</p>
58+
@endif
5059
</div>
5160
<div class="mb-4">
5261
<label class="form-label">Card Number</label>
53-
<input name="card_number" type="number" class="form-control">
62+
<input name="card_number" type="number" class="form-control {{$errors->has('card_number') ? 'is-invalid' : '' }}" value="{{old('card_number') ?: ''}}">
63+
@if ($errors->has('card_number'))
64+
<p class="text-danger">{{$errors->first('card_number')}}</p>
65+
@endif
5466
</div>
5567
<div class="mb-5">
5668
<div class="row">
5769
<div class="col-lg-6 col-12">
5870
<label class="form-label">Expired</label>
59-
<input name="expired" type="month" class="form-control">
71+
<input name="expired" type="month" class="form-control {{$errors->has('expired') ? 'is-invalid' : '' }}" value="{{old('expired') ?: ''}}">
72+
@if ($errors->has('expired'))
73+
<p class="text-danger">{{$errors->first('expired')}}</p>
74+
@endif
6075
</div>
6176
<div class="col-lg-6 col-12">
6277
<label class="form-label">CVC</label>
63-
<input name="cvc" type="number" class="form-control" maxlength="3">
78+
<input name="cvc" type="number" class="form-control {{$errors->has('cvc') ? 'is-invalid' : '' }}" maxlength="5" value="{{old('cvc') ?: ''}}">
79+
@if ($errors->has('cvc'))
80+
<p class="text-danger">{{$errors->first('cvc')}}</p>
81+
@endif
6482
</div>
6583
</div>
6684
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@if ($message = Session::get('error'))
2+
<div class="alert alert-danger alert-dismissible d-flex fade show" role="alert">
3+
<div class="px-2">
4+
<i class="uil uil-exclamation-triangle"></i>
5+
</div>
6+
<div>
7+
<strong>
8+
{{$message}}
9+
</strong>
10+
</div>
11+
<button class="btn-close" type="button" data-bs-dismiss="alert" aria-label="Close" ></button>
12+
</div>
13+
@endif

resources/views/layouts/app.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<title>@yield('title')</title>
1010

1111
<link rel="stylesheet" href="{{asset('frontend/css/bootstrap.min.css')}}" />
12+
<link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">
1213

1314
<link rel="stylesheet" href="{{asset('frontend/style/main1.css')}}" />
1415

resources/views/user/dashboard.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</div>
1919
</div>
2020
<div class="row my-5">
21+
@include('components.alert')
2122
<table class="table">
2223
<tbody>
2324
@forelse ($checkouts as $checkout)

0 commit comments

Comments
 (0)