4
4

More than 3 years have passed since last update.

Laravelのフォーム、バリデーション・入力値の保持(Blade書き方メモ)

Posted at

Laravelのフォームについての個人的なメモです。

Bootstrapのフォーム

・まとめたい要素form-control(label、option、selectなど)はform-gruopにまとめる。

バリデーション

@errorディレクティブを使う。引数にはチェックする項目を。
・フォームタグ内にrequired属性を追加することで、ブラウザによるバリデーションを行う。

入力値の保持

old()と三項演算子でテーブルに登録されている値を入れる。
autocompleteには自動入力する値を設定。

input

<div class="form-gruop">
 <label for="title">Title</label>
 <input id="title" class="form-control @error('title') is-invalid @enderror" name="title" required autocomplete="title" value="{{ old('title') ? : $articles->title }}"> 

 @error('title')
   <span class="invalid-feedback" role="alert">
      <strong>{{ $message }}</strong>
   </span>
 @enderror
</div>

textarea

<div class="form-gruop">
 <label for="Text">Text</label>
 <textarea class="form-control @error('text') is-invalid @enderror" name="text" required autocomplete="text" rows="4">{{ old('text') ? : $articles->text }}</textarea>

 @error('text')
   <span class="invalid-feedback" role="alert">
      <strong>{{ $message }}</strong>
   </span>
 @enderror
</div>

select

<div class="form-gruop">
 <label for="num">Number</label>
 <select name="num" id="genre" class="form-control @error('num') is-invalid @enderror" required>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 </select>

 @error('num')
   <span class="invalid-feedback" role="alert">
      <strong>{{ $message }}</strong>
   </span>
 @enderror
</div>
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4