チェックイン画面でクエリパラメータを受け付ける (#6)

* チェックイン画面でクエリパラメータを受け付けるようにした

* バリデーションエラー吐いたときにクエリパラメータが蘇らないようにした
This commit is contained in:
Shibafu 2018-03-06 22:43:49 +09:00 committed by GitHub
parent 277ee90379
commit 5b2427a2c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 12 deletions

View File

@ -12,9 +12,18 @@ use Illuminate\Support\Facades\Auth;
class EjaculationController extends Controller class EjaculationController extends Controller
{ {
public function create() public function create(Request $request)
{ {
return view('ejaculation.checkin'); $defaults = [
'date' => $request->input('date', date('Y/m/d')),
'time' => $request->input('time', date('H:i')),
'link' => $request->input('link', ''),
'tags' => $request->input('tags', ''),
'note' => $request->input('note', ''),
'is_private' => $request->input('is_private', 0) == 1
];
return view('ejaculation.checkin')->with('defaults', $defaults);
} }
public function store(Request $request) public function store(Request $request)
@ -24,7 +33,7 @@ class EjaculationController extends Controller
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
} }
Validator::make($inputs, [ $validator = Validator::make($inputs, [
'date' => 'required|date_format:Y/m/d', 'date' => 'required|date_format:Y/m/d',
'time' => 'required|date_format:H:i', 'time' => 'required|date_format:H:i',
'note' => 'nullable|string|max:500', 'note' => 'nullable|string|max:500',
@ -38,7 +47,11 @@ class EjaculationController extends Controller
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。'); $validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
} }
} }
})->validate(); });
if ($validator->fails()) {
return redirect()->route('checkin')->withErrors($validator)->withInput();
}
$ejaculation = Ejaculation::create([ $ejaculation = Ejaculation::create([
'user_id' => Auth::id(), 'user_id' => Auth::id(),
@ -98,7 +111,7 @@ class EjaculationController extends Controller
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
} }
Validator::make($inputs, [ $validator = Validator::make($inputs, [
'date' => 'required|date_format:Y/m/d', 'date' => 'required|date_format:Y/m/d',
'time' => 'required|date_format:H:i', 'time' => 'required|date_format:H:i',
'note' => 'nullable|string|max:500', 'note' => 'nullable|string|max:500',
@ -112,7 +125,11 @@ class EjaculationController extends Controller
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。'); $validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
} }
} }
})->validate(); });
if ($validator->fails()) {
return redirect()->route('checkin')->withErrors($validator)->withInput();
}
$ejaculation->fill([ $ejaculation->fill([
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),

View File

@ -13,7 +13,7 @@
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
<label for="date"><span class="oi oi-calendar"></span> 日付</label> <label for="date"><span class="oi oi-calendar"></span> 日付</label>
<input id="date" name="date" type="text" class="form-control {{ $errors->has('date') || $errors->has('datetime') ? ' is-invalid' : '' }}" <input id="date" name="date" type="text" class="form-control {{ $errors->has('date') || $errors->has('datetime') ? ' is-invalid' : '' }}"
pattern="^20[0-9]{2}/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])$" value="{{ old('date') ?? date('Y/m/d') }}" required> pattern="^20[0-9]{2}/(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])$" value="{{ old('date') ?? $defaults['date'] }}" required>
@if ($errors->has('date')) @if ($errors->has('date'))
<div class="invalid-feedback">{{ $errors->first('date') }}</div> <div class="invalid-feedback">{{ $errors->first('date') }}</div>
@ -22,7 +22,7 @@
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
<label for="time"><span class="oi oi-clock"></span> 時刻</label> <label for="time"><span class="oi oi-clock"></span> 時刻</label>
<input id="time" name="time" type="text" class="form-control {{ $errors->has('time') || $errors->has('datetime') ? ' is-invalid' : '' }}" <input id="time" name="time" type="text" class="form-control {{ $errors->has('time') || $errors->has('datetime') ? ' is-invalid' : '' }}"
pattern="^([01][0-9]|2[0-3]):[0-5][0-9]$" value="{{ old('time') ?? date('H:i') }}" required> pattern="^([01][0-9]|2[0-3]):[0-5][0-9]$" value="{{ old('time') ?? $defaults['time'] }}" required>
@if ($errors->has('time')) @if ($errors->has('time'))
<div class="invalid-feedback">{{ $errors->first('time') }}</div> <div class="invalid-feedback">{{ $errors->first('time') }}</div>
@ -36,7 +36,7 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<input name="tags" type="hidden" value="{{ old('tags') }}"> <input name="tags" type="hidden" value="{{ old('tags') ?? $defaults['tags'] }}">
<label for="tagInput"><span class="oi oi-tags"></span> タグ</label> <label for="tagInput"><span class="oi oi-tags"></span> タグ</label>
<div class="form-control {{ $errors->has('tags') ? ' is-invalid' : '' }}"> <div class="form-control {{ $errors->has('tags') ? ' is-invalid' : '' }}">
<ul id="tags" class="list-inline d-inline"></ul> <ul id="tags" class="list-inline d-inline"></ul>
@ -54,7 +54,7 @@
<div class="form-row"> <div class="form-row">
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<label for="link"><span class="oi oi-link-intact"></span> オカズリンク</label> <label for="link"><span class="oi oi-link-intact"></span> オカズリンク</label>
<input id="link" name="link" type="text" class="form-control {{ $errors->has('link') ? ' is-invalid' : '' }}" placeholder="http://..." value="{{ old('link') }}"> <input id="link" name="link" type="text" class="form-control {{ $errors->has('link') ? ' is-invalid' : '' }}" placeholder="http://..." value="{{ old('link') ?? $defaults['link'] }}">
<small class="form-text text-muted"> <small class="form-text text-muted">
オカズのURLを貼り付けて登録することができます。 オカズのURLを貼り付けて登録することができます。
</small> </small>
@ -66,7 +66,7 @@
<div class="form-row"> <div class="form-row">
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<label for="note"><span class="oi oi-comment-square"></span> ノート</label> <label for="note"><span class="oi oi-comment-square"></span> ノート</label>
<textarea id="note" name="note" class="form-control {{ $errors->has('note') ? ' is-invalid' : '' }}" rows="4">{{ old('note') }}</textarea> <textarea id="note" name="note" class="form-control {{ $errors->has('note') ? ' is-invalid' : '' }}" rows="4">{{ old('note') ?? $defaults['note'] }}</textarea>
<small class="form-text text-muted"> <small class="form-text text-muted">
最大 500 文字 最大 500 文字
</small> </small>
@ -80,7 +80,7 @@
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<div class="form-check"> <div class="form-check">
<label class="custom-control custom-checkbox"> <label class="custom-control custom-checkbox">
<input name="is_private" type="checkbox" class="custom-control-input" {{ old('is_private') ? 'checked' : '' }}> <input name="is_private" type="checkbox" class="custom-control-input" {{ old('is_private') || $defaults['is_private'] ? 'checked' : '' }}>
<span class="custom-control-indicator"></span> <span class="custom-control-indicator"></span>
<span class="custom-control-description"> <span class="custom-control-description">
<span class="oi oi-lock-locked"></span> このチェックインを非公開にする <span class="oi oi-lock-locked"></span> このチェックインを非公開にする