1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5 $validatorとエラー

1
Posted at

自分用にメモ

controller
      $validator = $this->validator($request->all(), $validationArray);
      if ($validator->fails()) {
        dd(
          '1)$validator:',$validator
        , '2)$validator->errors():',$validator->errors() //よく使う。
        , '3)$validator->errors()->all():',$validator->errors()->all() //配列になる。あんまり使えない?
        , '4)$validator->errors()->first("number"):',$validator->errors()->first('number') //viewにエラーメッセージ出す用
        , '5)$validator->messages():',$validator->messages() 
        );
      }

結果

"1)$validator:"
Validator {#294 ▼
  #translator: Translator {#298 ▶}
  #presenceVerifier: DatabasePresenceVerifier {#297 ▶}
  #container: Application {#3 ▶}
  #failedRules: array:2 [▶]
  #messages: MessageBag {#300 ▶}
  #data: array:13 [▶]
  #files: []
  #rules: array:3 [▶]
  #after: []
  #customMessages: []
  #fallbackMessages: []
  #customAttributes: []
  #customValues: []
  #extensions: []
  #replacers: []
  #sizeRules: array:4 [▶]
  #numericRules: array:2 [▶]
  #implicitRules: array:8 [▶]
}


"2)$validator->errors():"
MessageBag {#300 ▼
  #messages: array:2 [▼
    "number" => array:1 [▼
      0 => "「電話番号」は必須です"
    ]
    "email" => array:1 [▼
      0 => "「Email」は必須です"
    ]
  ]
  #format: ":message"
}


"3)$validator->errors()->all():"
array:2 [▼
  0 => "「電話番号」は必須です"
  1 => "「Email」は必須です"
]


"4)$validator->errors()->first("number"):"
"「電話番号」は必須です"


"5)$validator->messages():"
MessageBag {#300 ▼
  #messages: array:2 [▼
    "number" => array:1 [▼
      0 => "「電話番号」は必須です"
    ]
    "email" => array:1 [▼
      0 => "「Email」は必須です"
    ]
  ]
  #format: ":message"
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?