0
0

More than 3 years have passed since last update.

配列で受け取ったRequestに対してのバリデーション

Posted at
  <table>
    <form action="/person/create" method="post">
      {{ csrf_field() }}
      <tr>
        <th>name: </th>
        <td><input type="text" name="person[name]" value="{{ old('name') }}"></td>
      </tr>
      <tr>
        <th>email: </th>
        <td><input type="text" name="person[email]" value="{{ old('email') }}"></td>
      </tr>
      <tr>
        <th>age: </th>
        <td><input type="text" name="person[age]" value="{{ old('age') }}"></td>
      </tr>
      <tr>
        <th></th>
        <td>
        <input type="submit" value="send">
        </td>
      </tr>
    </form>
  </table>

上記のように配列にまとめてname="person[name]のような形でフォームを送信する場合

バリデーションの書き方が少し変わる
・配列名.name
・配列名.email
上記のような指定方法となる

  // バリデーション  //

  # requestが配列で送られてくる場合は'配列.要素名'となる
  public static $rules = array(
    'person.name' => 'required',
    'person.email' => 'email',
    'person.age' => 'integer|min:0|max:150'
  );
0
0
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
0
0