[英]Laravel validation - comma separated string input as array
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?任何想法有什么问题?
I was running into a similar problem with comma separated emails on 5.4.我在 5.4 上遇到了用逗号分隔的电子邮件的类似问题。 Here's how I solved it:
这是我解决它的方法:
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
我做了一个要点,如果你也感兴趣的话
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'));
}
Instead of mutating your input, for Laravel 6+ there is a package to apply these validations to a comma separated string of values: Laravel 6+ 有一个包可以将这些验证应用于逗号分隔的值字符串,而不是改变您的输入:
https://github.com/spatie/laravel-validation-rules#delimitedhttps://github.com/spatie/laravel-validation-rules#delimited
You need toinstall it :你需要安装它:
composer require spatie/laravel-validation-rules
Then, you can use it as a rule (using a FormRequest
is recommended).然后,您可以将其用作规则(建议使用
FormRequest
)。
For example, to validate all items are emails, not long of 20 characters and there are at least 3 of them, you can use:例如,要验证所有项目都是电子邮件,长度不超过 20 个字符并且至少有 3 个字符,您可以使用:
use Spatie\ValidationRules\Rules\Delimited;
// ...
public function rules()
{
return [
'emails' => [(new Delimited('email|max:20'))->min(3)],
];
}
The constructor of the validator accepts a validation rule string, a validation instance, or an array.验证器的构造函数接受验证规则字符串、验证实例或数组。 That rules are used to validate all separate values;
该规则用于验证所有单独的值; in this case,
'email|max:20'
.在这种情况下,
'email|max:20'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.