可以給定一組資料用Validator 驗證嗎
public function store(PincardRequest $request){
这个是方法
}
下面這個是驗證
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class PincardRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'yd'=>array('required','regex:/\p{Han}/u'),
];
}
public function messages(){
return [
'yd.required'=>'不能为空!',
];
}
}
我要怎麼在store傳yd過去給驗證
怪我咯2017-05-16 16:50:34
謝邀,可以這麼做,在控制器中增加下面兩個方法,如果需要傳數組驗證,則使用下面的方法
/**
* Validate the given parameters with the given rules.
*
* @param array $parameters
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function validateParameters($parameters, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->getValidationFactory()->make($parameters, $rules, $messages, $customAttributes);
if ($validator->fails()) {
$this->throwValidationException(app('request'), $validator);
}
}
/**
* 抛出一个验证异常
*
* @param WrapedValidationException $e
* @param Request $request
*
* @throws ValidationException
*/
protected function throwWrapedValidationException(WrapedValidationException $e, Request $request)
{
throw new ValidationException(null, $this->buildFailedValidationResponse($request, $e->getErrors()));
}