Laravel 验证 - 以逗号分隔的字符串输入作为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46613988/
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 validation - comma separated string input as array
提问by general666
How can I validate one input with multiple values? I'm using bootstrap tagsinput plugin. It returns all tags in one field. I need to validate this tags - unique. First I'm trying to place this tags into array and then validate it in request but still no luck. Here is my code in request:
如何使用多个值验证一个输入?我正在使用引导标签输入插件。它返回一个字段中的所有标签。我需要验证这个标签 - 独一无二。首先,我试图将这个标签放入数组中,然后在请求中验证它,但仍然没有运气。这是我的请求代码:
public function all()
{
$postData = parent::all();
// checkbox status
if(array_key_exists('keywords', $postData)) {
// put keywords into array
$keywords = explode(',', $postData['keywords']);
$test = [];
$i = 0;
foreach($keywords as $keyword)
{
$test[$i] = $keyword;
$i++;
}
$postData['keywords'] = $test;
}
return $postData;
}
public function rules()
{
$rules = [
'title' => 'required|min:3|unique:subdomain_categories,title|unique:subdomain_keywords,keyword',
'description' => '',
'image' => 'required|image',
'keywords.*' => 'min:3'
];
return $rules;
}
But as soon as keyword becomes invalid I get this error:
但是一旦关键字无效,我就会收到此错误:
ErrorException in helpers.php line 531: htmlentities() expects parameter 1 to be string, array given.
helpers.php 第 531 行中的 ErrorException:htmlentities() 期望参数 1 为字符串,给定数组。
Any ideas what's wrong?
任何想法有什么问题?
回答by zerodahero
I was running into a similar problem with comma separated emails on 5.4. Here's how I solved it:
我在 5.4 上遇到了逗号分隔电子邮件的类似问题。这是我解决它的方法:
In your request class, override the prepareForValidation()
method (which does nothing by default, btw), and explode your comma separated string.
在您的请求类中,覆盖该prepareForValidation()
方法(默认情况下不执行任何操作,顺便说一句),并分解逗号分隔的字符串。
/**
* @inheritDoc
*/
protected function prepareForValidation()
{
$this->replace(['keywords' => explode(',', $this->keywords)]);
}
Now you can just use Laravel's normal array validation!
现在你可以使用 Laravel 的普通数组验证了!
Since my case needed to be a bit more explicit on the messages, too, I added in some attribute and message customizations as well. I made a gist, if that's of interest to you as well
由于我的案例也需要对消息更加明确,因此我还添加了一些属性和消息自定义。我做了一个要点,如果你也感兴趣的话
回答by Webdesigner
It's not a good practice to override the all()
method to validate a field.
重写all()
验证字段的方法不是一个好习惯。
If you receive a comma separated String and not an array that you want to validate, but there is no common Method available, write your own.
如果您收到逗号分隔的字符串而不是您要验证的数组,但没有可用的通用方法,请编写您自己的方法。
in your App/Providers/ValidatorServiceProvider
just add a new Validation:
在您App/Providers/ValidatorServiceProvider
只需添加一个新的验证:
public function boot()
{
Validator::extend('keywords', function ($attribute, $value, $parameters, $validator)
{
// put keywords into array
$keywords = explode(',', $value);
foreach($keywords as $keyword)
{
// do validation logic
if(strlen($keyword) < 3)
{
return false;
}
}
return true;
}
}
Now you can use this Validation Rule on any request if needed, it is reusable.
现在,如果需要,您可以在任何请求上使用此验证规则,它是可重用的。
public function rules()
{
$rules = [
'title' => 'required|min:3|unique:subdomain_categories,title|unique:subdomain_keywords,keyword',
'description' => 'nullable',
'image' => 'required|image',
'keywords' => 'keywords',
];
return $rules;
}
If the validation pass and you want to make the keywords be accessible in your request as array, write your own method:
如果验证通过并且您想让关键字在您的请求中作为数组可访问,请编写您自己的方法:
public function keywords ()
{
return explode(',', $this->get('keywords'));
}