Laravel 5请求-更改数据 [英] Laravel 5 Request - altering data
问题描述
我遇到一个实例,我需要更改需要验证的数据,即,当没有提交任何数据块时,请从标题中创建一个数据,然后验证它是否唯一.
I have come across an instance where I need to alter the data that needs to be validated i.e. when no slug has been submitted, create one from the title and then validate that it is unique.
请求具有方法replace()
,该方法应该替换请求上的输入数据,但似乎无法正常工作.谁能对此有所启发?这就是我所拥有的:
The request has a method replace()
which is supposed to replace the input data on the request but it doesn't appear to work. Can anyone shine a light on this? This is what I have:
<?php namespace Purposemedia\Pages\Http\Requests;
use Dashboard\Http\Requests\Request;
use Illuminate\Auth\Guard;
class PageRequest extends Request {
/**
* [authorize description]
* @return {[type]} [description]
*/
public function authorize( Guard $auth) {
return true;
}
/**
* [rules description]
* @return {[type]} [description]
*/
public function rules() {
// Get all data from request
$data = $this->request->all();
if( $data['slug'] === '' ) {
// if the slug is blank, create one from title data
$data['slug'] = str_slug( $data['title'], '-' );
// replace the request data with new data to validate
$this->request->replace( $data );
}
return [
'title' => 'required|min:5|max:255',
'slug' => 'min:5|max:255|alpha_dash|unique:pages,slug' . $this->getSegmentFromEnd(),
];
}
}
推荐答案
您应该在formatInput
方法中进行操作.您通过此方法返回的数据将用于验证器检查:
You should do it in formatInput
method. Data you return in this method will be used for validator to check:
例如:
protected function formatInput()
{
$data = $this->all();
if( $data['slug'] === '') {
// if the slug is blank, create one from title data
$data['slug'] = str_slug( $data['title'], '-' );
}
return $data;
}
编辑
此方法是2个月前在Laravel中使用的(我仍在使用此版本),这很奇怪,它被删除了.
This method was in Laravel 2 months ago (I'm still using this version), that's quite strange it was removed.
将上述方法更改为:
public function all()
{
$data = parent::all();
if( $data['slug'] === '') {
// if the slug is blank, create one from title data
$data['slug'] = str_slug( $data['title'], '-' );
}
return $data;
}
EDIT2
我把整个工作的TestRequest
类放在这里.我发送空数据,并且由于修改了all()
方法,即使是空数据,验证仍通过,因为我手动设置了这些数据进行验证.如果删除此all()
方法并发送空数据,我当然会显示错误
I put here the whole working TestRequest
class. I send empty data and because of modified all()
method even if empty data, validation passes because I manually set those data to validate. If I remove this all()
method and send empty data of course I will get errors displayed
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class TestRequest 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 [
//
'title' => 'required|min:2|max:255',
'slug' => 'min:2|max:255'
];
}
public function response(array $errors){
dd($errors);
}
public function all(){
$data = parent::all();
$data['title'] = 'abcde';
$data['slug'] = 'abcde';
return $data;
}
}
这篇关于Laravel 5请求-更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!