我正在尝试在laravel中进行枚举列验证。这是我的验证程序的代码。
pip3 install --upgrade pip
pip3 install --upgrade pip
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main
枚举列是/**
* Returns the rules and messages for validating this creation
*/
public static function ValidationBook($except = [], $append = []) {
$book = ['rules' => [], 'messages' => []];
$arr = config('constants.publication_statuses');
$arrKeys = array_keys($arr);
$book['rules'] = [
'concert.title' => 'required|string',
'concert.user_id' => 'required|exists:users,id',
'concert.type' => [
'required',
Rule::in(['public', 'private']),
],
'concert.status' => 'required',
'concert.closes_on' => 'nullable'
];
$book['messages'] = [
'concert.title.required' => 'El título es requerido.',
'concert.title.string' => 'El título debe ser un texto',
'concert.user_id.exists' => 'Se debe ingresar un usuario válido.',
'concert.type.required' => 'El tipo es requerido.',
'concert.status.required' => 'El status es requerido.',
];
if (!empty($except)) {
$except = array_flip($except);
$book['rules'] = array_diff_key($book['rules'], $except);
}
if (!empty($append)) {
$book = array_merge_recursive($book, $append);
}
return $book;
}
列。我也尝试做type
然后,我使用以下代码创建验证器:
'concert.type' => 'required|in:public,private'
但是由于某种原因,当我通过邮递员发送帖子时,我得到了$vb = Concert::ValidationBook($except, $append);
$validator = Validator::make($data, $vb['rules'], $vb['messages']);
。
即使我的数据是:
"detail": "Undefined index: concert.type"
预先感谢
答案 0 :(得分:1)
通常会在数据库中设置枚举,而前端将是一个下拉列表
$ table-> enum('concert_type',['public','private']);
但是您可以尝试此解决方案
'concert_type' => 'in:public,private',
//公共或私有值