LoginSignup
6
3

More than 3 years have passed since last update.

自分メモ:バリデーションを行うのはサーバー側?クライアント側?両方??

Posted at

結論:サーバー側、クライアント側の両方行います。

(twitter引用:https://twitter.com/saya1001kirinn/status/1191950370328715264)

例えばLaravelで最小値0を指定するバリデーションの場合、Controllerのバリデーションにmin:0って書くのか、viewでinputタグ内にmin="0"を書くのか。

例:サーバー側対応

Controller.php
$request->validate([
    'total' => ['required', 'min:0'],
]);

例:クライアント側対応

create.blade.php
<input type="number" min="1" value="{{ old('total') }}">

サーバー側はデータの整合性を担保するため、クライアント側はUXを向上するためと考えます。
サーバー側の対応は必須です。

時間がない場合はサーバー側のみの対応でもいいかと思いますが、
可能であれば両方対応が良さそうです。
クライアント側はHTMLバリデーションまたはjsでバリデーションを行います。

6
3
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
6
3