在我的Laravel 5.7应用中使用spatie/laravel-tags plugin。
我担任tags
的编辑者,以便管理员可以添加tags
。但是,我需要对tags
的唯一性进行验证。
常用的验证规则
'name' => [
'required',
'string',
'max:255',
Rule::unique(with(new Tag)->getTableName())->ignore($tag_id),
],
'order_column' => 'nullable|integer',
在这里不起作用,因为字段采用json格式,例如:
{"en": "Drama"}
能否请您提示该验证的哪种方式?
已修改的2号区块
在我的请求app/Http/Requests/TagRequest.php
中,我编写了验证规则:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Rules\TagUniqueness;
class TagRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
// $request= Request();
return [
'name' => [
'required',
'string',
'max:255',
new TagUniqueness,
],
'order_column' => 'nullable|integer',
];
}
}
创建的规则app/Rules/TagUniqueness.php
具有:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class TagUniqueness implements Rule
{
public function __construct()
{
$this->d( '<pre>TagUniqueness $_REQUEST::' . print_r( $_REQUEST, true ) );
}
public function passes($attribute, $value)
{
$this->d( '<pre>passes $attribute::' . print_r( $attribute, true ) );
$this->d( '<pre>passes $value::' . print_r( $value, true ) );
return false;
}
...
我收到此错误:
"message": "trim() expects parameter 1 to be string, object given",
"exception": "ErrorException",
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 217,
"trace": [
{
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 217,
"function": "trim"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 199,
"function": "parseArrayRule",
"class": "Illuminate\\Validation\\ValidationRuleParser",
"type": "::"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 161,
"function": "parse",
"class": "Illuminate\\Validation\\ValidationRuleParser",
"type": "::"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 144,
"function": "purgeNonRemoteRules",
"class": "Proengsoft\\JsValidation\\Remote\\Validator",
"type": "->"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 116,
"function": "setRemoteValidation",
"class": "Proengsoft\\JsValidation\\Remote\\Validator",
"type": "->"
},
{
作为$ this-> d(我的调试方法,我看到TagUniqueness的构造函数的输出,但是传递的方法未调用,我无法定义此错误的原因。
怎么了?
谢谢!