在 laravel formRequests 中获取输入值
Get input values in laravel formRequests
这是我的 FormRequest class
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use URL;
class CaseStudyUpdateRequest extends FormRequest
{
/**
* 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()
{
$id = $this->request->get('cspid');
dump($this);
dump($id);
$prev_url = URL::previous();
$url_arr = explode('/', $prev_url);
$data = $this->request->all();
dump($data);
$rules = [
// 'post_title' => 'required|min:15|max:255|unique:case_study_posts,title,'.$id,
'summary' => "required|array|min:1",
'total_blocks' => "not_in:0",
'category' => 'required',
'specialities' => 'required',
];
if($url_arr[4] === "edit-for-request") {
// $rules['edit_comment'] = 'required';
if(!empty($data)) {
$rules['edit_comment'] = 'required';
$totalBlocks = $data['total_blocks'];
foreach(range(1, $totalBlocks) as $i){
if(array_key_exists("description-$i", $data)) {
// $rules['edit_comment'] = 'required';
$rules["description-$i"] = 'required|min:15';
} elseif(array_key_exists("image-$i", $data) || array_key_exists("image_title-$i", $data) || array_key_exists("image_description-$i", $data)) {
// $rules['edit_comment'] = 'required';
$rules["image-$i"] = (array_key_exists("old-img-$i", $data) && !array_key_exists("image-$i", $data)?"":"required|mimes:jpeg,bmp,jpg,png|max:2048");
$rules["image_title-$i"] = "required_with:image-$i|required|min:5|max:30";
$rules["image_description-$i"] = "required_with:image_title-$i|required|min:15|max:120";
}
}
}
}
return $rules;
}
public function messages() {
return [
'total_blocks'=> 'Please select text/images block to case studys',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes() {
return [
'post_title' => 'Case Study Title',
];
}
}
?>
查看页面
在我的视图页面中,我有这个,
@section('scripts')
@parent
{{ Html::script('vendor/jsvalidation/js/jsvalidation.js') }}
{!! JsValidator::formRequest('App\Http\Requests\CaseStudyUpdateRequest', '#edit_post_form'); !!}
....
....
控制器
在 Controller 中,我添加了
use App\Http\Requests\CaseStudyUpdateRequest;
在被调用的函数中
public function editCaseStudy(CaseStudyUpdateRequest $request) {
在这里,我有图像块和描述块,比如图像 1、图像 2、图像 3、描述 1、描述 2 等。
我想验证这些字段,所以我需要在 formRequest 文件中获取这些输入值。但是当我打印 dump($this->request->all());
时它显示一个空数组
如何在端 formRequest 中获取输入值?
提前致谢
Illuminate\Foundation\Http\FormRequest
class extends the Illuminate\Http\Request
class. So all()
or any of the usual methods 将允许您从 GET 或 POST.
访问请求参数
例如而不是这个:
$id = $this->request->get('cspid');
使用其中之一:
$id = $this->cspid;
$id = $this->get("cspid");
在表单请求class中,rules()函数:
$this->route()->parameter('myrouteparam')
这是我的 FormRequest class
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use URL;
class CaseStudyUpdateRequest extends FormRequest
{
/**
* 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()
{
$id = $this->request->get('cspid');
dump($this);
dump($id);
$prev_url = URL::previous();
$url_arr = explode('/', $prev_url);
$data = $this->request->all();
dump($data);
$rules = [
// 'post_title' => 'required|min:15|max:255|unique:case_study_posts,title,'.$id,
'summary' => "required|array|min:1",
'total_blocks' => "not_in:0",
'category' => 'required',
'specialities' => 'required',
];
if($url_arr[4] === "edit-for-request") {
// $rules['edit_comment'] = 'required';
if(!empty($data)) {
$rules['edit_comment'] = 'required';
$totalBlocks = $data['total_blocks'];
foreach(range(1, $totalBlocks) as $i){
if(array_key_exists("description-$i", $data)) {
// $rules['edit_comment'] = 'required';
$rules["description-$i"] = 'required|min:15';
} elseif(array_key_exists("image-$i", $data) || array_key_exists("image_title-$i", $data) || array_key_exists("image_description-$i", $data)) {
// $rules['edit_comment'] = 'required';
$rules["image-$i"] = (array_key_exists("old-img-$i", $data) && !array_key_exists("image-$i", $data)?"":"required|mimes:jpeg,bmp,jpg,png|max:2048");
$rules["image_title-$i"] = "required_with:image-$i|required|min:5|max:30";
$rules["image_description-$i"] = "required_with:image_title-$i|required|min:15|max:120";
}
}
}
}
return $rules;
}
public function messages() {
return [
'total_blocks'=> 'Please select text/images block to case studys',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes() {
return [
'post_title' => 'Case Study Title',
];
}
}
?>
查看页面
在我的视图页面中,我有这个,
@section('scripts')
@parent
{{ Html::script('vendor/jsvalidation/js/jsvalidation.js') }}
{!! JsValidator::formRequest('App\Http\Requests\CaseStudyUpdateRequest', '#edit_post_form'); !!}
....
....
控制器
在 Controller 中,我添加了
use App\Http\Requests\CaseStudyUpdateRequest;
在被调用的函数中
public function editCaseStudy(CaseStudyUpdateRequest $request) {
在这里,我有图像块和描述块,比如图像 1、图像 2、图像 3、描述 1、描述 2 等。
我想验证这些字段,所以我需要在 formRequest 文件中获取这些输入值。但是当我打印 dump($this->request->all());
如何在端 formRequest 中获取输入值? 提前致谢
Illuminate\Foundation\Http\FormRequest
class extends the Illuminate\Http\Request
class. So all()
or any of the usual methods 将允许您从 GET 或 POST.
例如而不是这个:
$id = $this->request->get('cspid');
使用其中之一:
$id = $this->cspid;
$id = $this->get("cspid");
在表单请求class中,rules()函数:
$this->route()->parameter('myrouteparam')