Laravel 5.5-验证数组内部对象中的字符串

时间:2018-10-08 10:34:48

标签: laravel-5.5

我有请求:

`array:2 [
  "alt_tags" => "1"
  "name_pattern" => array:2 [
    0 => "{"label":"Main Title","value":"main_title"}"
    1 => "{"label":"Store Name","value":"store_name"}"
  ]
]`

在Request类中,我尝试通过以下方式验证对象内部的行:

`$labels = \implode(',', [
        'Main Title',
        'Store Name',
    ]);

 $values = \implode(',', [
        'main_title',
        'store_name',
    ]);

 return [
        'alt_tags'             => 'required|boolean',
        'name_pattern'         => 'required|array|min:0|max:2',
        'name_pattern.*.label' => "sometimes|string|in:{$labels}",
        'name_pattern.*.value' => "sometimes|string|in:{$values}",
        ];`

但是由于字符串在对象内部,所以无法通过file_name_pattern. *.label

data_get(request()->all(), 'name_pattern.0')        // "{"label":"Main Title","value":"main_title"}"
data_get(request()->all(),'name_pattern.0.label');  // return NULL

0 个答案:

没有答案