【发布时间】:2016-04-26 05:01:46
【问题描述】:
我是 Laravel 新手,很抱歉我的新手问题:
我在输入字段中,只有当它为零或它的值存在于“产品”数据库表中时才有效。 我在 Laravel 5.2 中使用表单请求验证。
这是我的代码:
class CreateRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'product_id' => 'sometimes|exists:products,id'
];
}
}
这适用于数据库验证,但如果它为零,我怎样才能使它有效?
感谢您的帮助!
【问题讨论】:
-
我建议为此编写一个自定义验证,很简单:laravel.com/docs/5.2/validation#custom-validation-rules
标签: validation laravel