Created
December 7, 2016 17:52
-
-
Save itzikbenh/cf21cdaa62265a147758596a2afb179f to your computer and use it in GitHub Desktop.
Laravel validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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