Laravel 数组验证
Laravel Array Validation
$rules = [
'user_id' => 'required|exists:users,id',
'preparat_id' => 'required|exists:preparats,id',
'zoom' => 'required|numeric',
'comment' => '',
'type' => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,
'point_x' => 'array|required|numeric',
'point_y' => 'array|required|numeric',
];
$this->validate($request, $rules);
point_x
和 point_y
是数组输入。
我的规则是:
point_x
和 point_y
必须存在。
我如何发送数据:
- point_x[0] = 123;
- point_y[0] = 123;
真
- point_x[0] = 123;
- point_y[0] = 123;
- point_x[1] = 123;
- point_y[1] = 123;
- point_x[2] = 123;
- point_y[2] = 123;
真
point_x[0] = 123;
point_y[0] = "SO";
错误
point_y[0] = 123;
错误
- point_x[0] = 123;
- point_y[0] = 123;
- point_x[1] = 123;
- point_y[1] = "Taadaa";
- point_x[2] = 123;
- point_y[2] = 123;
错误
我的Laravel版本是5.4
我应该如何写一个像上面这样的检查规则。我尝试了数组参数,但它不起作用。
public function rules($array)
{
$rules = [
'user_id' => 'required|exists:users,id',
'preparat_id' => 'required|exists:preparats,id',
'zoom' => 'required|numeric',
'comment' => '',
'type' => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,];
foreach($array as $key => $val)
{
$rules[$key] = 'required|numeric';
}
return $rules;
}
仅尝试遵循数组 point_x
和 point_y
的规则,
$this->validate($request, [
'point_x' => 'required',
'point_y' => 'required',
'point_x.*' => 'numeric',
'point_y.*' => 'numeric',
],
$messages = [
]
);
$rules = [
'user_id' => 'required|exists:users,id',
'preparat_id' => 'required|exists:preparats,id',
'zoom' => 'required|numeric',
'comment' => '',
'type' => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,
'point_x' => 'array|required|numeric',
'point_y' => 'array|required|numeric',
];
$this->validate($request, $rules);
point_x
和 point_y
是数组输入。
我的规则是:
point_x
和 point_y
必须存在。
我如何发送数据:
- point_x[0] = 123;
- point_y[0] = 123;
真
- point_x[0] = 123;
- point_y[0] = 123;
- point_x[1] = 123;
- point_y[1] = 123;
- point_x[2] = 123;
- point_y[2] = 123;
真
point_x[0] = 123; point_y[0] = "SO";
错误
point_y[0] = 123;
错误
- point_x[0] = 123;
- point_y[0] = 123;
- point_x[1] = 123;
- point_y[1] = "Taadaa";
- point_x[2] = 123;
- point_y[2] = 123;
错误
我的Laravel版本是5.4
我应该如何写一个像上面这样的检查规则。我尝试了数组参数,但它不起作用。
public function rules($array)
{
$rules = [
'user_id' => 'required|exists:users,id',
'preparat_id' => 'required|exists:preparats,id',
'zoom' => 'required|numeric',
'comment' => '',
'type' => 'in:' . Annotation::ANN_RECTANGLE . ',' . Annotation::ANN_CIRCLE . ',' . Annotation::ANN_POLYGON . ',' . Annotation::ANN_PIN,];
foreach($array as $key => $val)
{
$rules[$key] = 'required|numeric';
}
return $rules;
}
仅尝试遵循数组 point_x
和 point_y
的规则,
$this->validate($request, [
'point_x' => 'required',
'point_y' => 'required',
'point_x.*' => 'numeric',
'point_y.*' => 'numeric',
],
$messages = [
]
);