-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aafc08
commit fbfe238
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
|
||
namespace JoyceZ\LaravelLib\Validation; | ||
|
||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
|
||
/** | ||
* 表单提交验证 | ||
* Class BaseRequest | ||
* @package JoyceZ\LaravelLib\Validation | ||
*/ | ||
abstract class BaseRequest extends FormRequest | ||
{ | ||
/** | ||
* 定义验证规则 | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$rule_action = 'getRulesBy' . ucfirst($this->route()->getActionMethod()); | ||
|
||
if (method_exists($this, $rule_action)) | ||
return $this->$rule_action(); | ||
|
||
return $this->getDefaultRules(); | ||
} | ||
|
||
/** | ||
* 默认验证规则 | ||
* @return array | ||
*/ | ||
protected function getDefaultRules() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* 验证消息通过,json抛出,api开发 | ||
* @param Validator $validator | ||
* @throws \HttpResponseException | ||
*/ | ||
protected function failedValidation(Validator $validator) | ||
{ | ||
throw new HttpResponseException(response()->json([ | ||
'code' => -1, | ||
'message' => $validator->errors()->first() | ||
])); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace $NAMESPACE$; | ||
|
||
use JoyceZ\LaravelLib\Validation\BaseRequest; | ||
|
||
/** | ||
* 请说明具体哪块业务的 Request | ||
* | ||
* Class $CLASS$Request | ||
* @package $NAMESPACE$ | ||
*/ | ||
class $CLASS$Request extends BaseRequest | ||
{ | ||
|
||
/** | ||
* 定义针对 Controller->store( )的验证规则 | ||
* @return array | ||
*/ | ||
public function getRulesByStore() | ||
{ | ||
return [ | ||
|
||
]; | ||
} | ||
|
||
/** | ||
* 定义针对 Controller->update( )的验证规则 | ||
* @return array | ||
*/ | ||
public function getRulesByUpdate() | ||
{ | ||
return [ | ||
|
||
]; | ||
} | ||
|
||
/** | ||
* 定义针对 Controller->delete( )的验证规则 | ||
* | ||
* @return array | ||
*/ | ||
public function getRulesByDelete() | ||
{ | ||
return [ | ||
]; | ||
} | ||
|
||
/** | ||
* 统一定义验证规则的自定义错误消息。 | ||
* @return array | ||
*/ | ||
public function messages() | ||
{ | ||
return [ | ||
|
||
]; | ||
} | ||
|
||
|
||
} |