Laravel 5 验证修剪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29386203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Validation Trim
提问by fajar ainul
I am a beginner in Laravel 5.
我是 Laravel 5 的初学者。
How can I remove whitespaces in validator?? i have read the documentation but there is no validator for trim(remove whitespaces).
如何删除验证器中的空格?我已阅读文档,但没有用于修剪(删除空格)的验证器。
here my rules
这是我的规则
$rules = [
'name' =>'required',
'email' => 'required|email',
'address' => 'required',
'phones' => 'required'
];
thanks for your answer.
感谢您的回答。
回答by Limon Monte
It's not job for validator to change any input data. Trim validator exists in CodeIgniter, but as to me this isn't right place to perform trim.
验证器不负责更改任何输入数据。修剪验证器存在于 CodeIgniter 中,但对我而言,这不是执行修剪的正确位置。
You can automatically trim all input by using using this:
您可以使用以下命令自动修剪所有输入:
Input::merge(array_map('trim', Input::all()));
Now do the rest of your coding:
现在做剩下的编码:
$username = Input::get('username'); // it's trimed
// ...
Validator::make(...);
回答by Alex
You can use the following code to trim all string input (as you might have arrays in the input)
您可以使用以下代码修剪所有字符串输入(因为您可能在输入中有数组)
// trim all input
Input::merge(array_map(function ($value) {
if (is_string($value)) {
return trim($value);
} else {
return $value;
}
}, Input::all()));
回答by Khan Muhammad
In Laravel 5.2 or 5.3 you can use trim for spaces remove like this
在 Laravel 5.2 或 5.3 中,您可以像这样使用修剪删除空格
$input = array_map('trim', $request->all());
$input = array_map('trim', $request->all());
so this will remove all space form inputs that posted and validation will work fine
所以这将删除发布的所有空间表单输入,验证将正常工作
回答by afzylive
public function formatInput()
{
$input = array_map('trim', $this->all());
$this->replace($input);
return $this->all();
}
回答by PHP Worm...
You can use this in your request file.
您可以在请求文件中使用它。
$input = request()->all();
$input['url'] = trim($input['url']);
request()->replace($input);
You can also create an all()
function in your request file to update request parameters:
您还可以all()
在请求文件中创建一个函数来更新请求参数:
public function all()
{
$all = parent::all(); // $this->all();
$all['new_key'] = "new_data";
$all['old_key'] = "new_data";
return $all;
}
回答by Farshad Fahimi
Due to the documention laravel HTTP Requestby default laravel trim all the requests data.
由于文档laravel HTTP Request默认情况下,laravel 会修剪所有请求数据。
and Trim
the request in validation part is so dirty job.
You could manage this feature like trim
or convert empty string to null
with the middleware
并Trim
在确认部分的要求是如此肮脏的工作。您可以像trim
或convert empty string to null
使用中间件一样管理此功能
because middleware execute before the validation and you could have clean data in validation
因为中间件在验证之前执行,您可以在验证中获得干净的数据
回答by msbrime
I extended the FormRequest
class and overrode the prepareForValidation
method which is called before validation happens.
我扩展了FormRequest
类并覆盖了prepareForValidation
在验证发生之前调用的方法。
// anything I don't want trimmed here
protected $untrimmable = [];
// replace the request with trimmed request here
protected function prepareForValidation()
{
return $this->replace($this->trimData($this->all()));
}
// recursively trim the fields in the request here
protected function trimData($data,$keyPrefix = '')
{
$trimmedFields = array_map(function($value,$field) use ($keyPrefix){
// if the value is an array handle it as
// a request array and send along the prefix
if(is_array($value)){
return $this->trimData($value,$this->dotIndex($keyPrefix,$field));
}
// if the field is not in the specified fields to be
// left untrimmed
if(
!in_array($this->dotIndex($keyPrefix,$field),$this->dontTrim) &&
!in_array($this->dotIndex($keyPrefix,$field), $this->untrimmable)
) {
return trim((string) $value);
}
return $value;
}, $data,array_keys($data));
return array_combine(array_keys($data),$trimmedFields);
}
What it does:
它能做什么:
- Replace request with a new one with trimmed inputs
- Set all fields I don't want trimmed in an
untrimmable
property. - Handles nested inputs with dot notation .
- 将请求替换为具有修剪输入的新请求
- 设置我不想在
untrimmable
属性中修剪的所有字段。 - 使用点符号处理嵌套输入。
Here's a link to the gist https://gist.github.com/msbrime/336a788c7cced2137bdc7896c1241239
这是要点的链接https://gist.github.com/msbrime/336a788c7cced2137bdc7896c1241239
回答by Deep Gandhi
$data = $r->all();
foreach ($data as $key => $value) {
if($value!=""){ //If your primaryKey id is autoincrement
$data[$key] = preg_replace('/\s{2,}/',' ',$value);
}
}
$variable = new modelName($data);
$variable->save();
//Here Parameter Explaination
=> '/\s{2,}/' Pattern must write between '/ /' and \s{2,} means 2 or more than 2 spaces sholud be trim
=> ' ' second parameter is with. Means 1st parameter replace with 2nd parameter(here, one space)
=> $value is variable which is trim