最近几年一直在用laravel框架,忽略了框架自身的验证类,细细研究,发现还挺好用,看了源码,发现参数的用途,第一个参数时接收到的参数,第二个参数是规则,第三个规则定义,第四个参数每个参数的自定义属性,可以写参数的汉字翻译,需要写上每个参数的规则
public function transferOut(Request $request){
$params = $request->all();
$validator = \Validator::make($params,[
'accountId' => 'required',
'type' => 'required',
'amount' => 'required',
],[
'required' => ':attribute不能为空'
],[
'accountId' => '用户ID',
'type' => '转出类型',
'amount' => '转出金额',
]);
if($params['amount'] <= 0){
return responseJson(400,'转出金额必须大于0元');
}
if ($validator->fails()) {
return ['code' => 400, 'msg' => $validator->errors()->first()];
}
return WalletServiceImpl::transferOut($params);
}
/**
* Create a new Validator instance.
*
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return \Illuminate\Validation\Validator
* @static
*/
public static function make($data, $rules, $messages = array(), $customAttributes = array())
{
return \Illuminate\Validation\Factory::make($data, $rules, $messages, $customAttributes);
}
|