Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Created December 7, 2016 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzikbenh/cf21cdaa62265a147758596a2afb179f to your computer and use it in GitHub Desktop.
Save itzikbenh/cf21cdaa62265a147758596a2afb179f to your computer and use it in GitHub Desktop.
Laravel validations
<?php
//One way
$this->validate($request,
[
'data.*.name' => 'unique:priorities'
],
[
'data.*.name.unique' => 'Priority name exists already, please pick a different name.'
]
);
//Second way
$validator = Validator::make($request->all(), [
'data.*.name' => 'unique:priorities',
]);
if ($validator->fails()) {
return response()->json(['formError' => ["Priority already exists, please use a different name."]], 422);
}
//In case you want the Laravel default message
if ($validator->fails()) {
return response()->json($validator->messages(), 422);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment