带参数的 Laravel 5 表单请求验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31443995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Form request validation with parameters
提问by Dipendra Gurung
I am using form request validation and there are some rules that needs external values as a parameters.
我正在使用表单请求验证,并且有一些规则需要外部值作为参数。
Here are my validation rules for editing a business profile inside a form request class,
这是我在表单请求类中编辑业务配置文件的验证规则,
public function rules()
{
return [
'name' => 'required|unique:businesses,name,'.$business->id,
'url' => 'required|url|unique:businesses'
];
}
I can use this on the controller by type hinting it.
我可以通过类型提示在控制器上使用它。
public function postBusinessEdit(BusinessEditRequest $request, Business $business) {
公共功能 postBusinessEdit(BusinessEditRequest $request, Business $business) {
}
}
But how to pass the $business object as a parameter to the rules method?
但是如何将 $business 对象作为参数传递给规则方法呢?
采纳答案by Sh1d0w
Lets say this is your model binding:
假设这是您的模型绑定:
$router->model('business', 'App\Business');
Then you can reference the Business
class from within the FormRequest
object like this:
然后,您可以像这样Business
从FormRequest
对象内部引用该类:
public function rules()
{
$business = $this->route()->getParameter('business');
// rest of the code
}
Note that if you use your form request both for create and update validation, while creating the record, the business variable will be null
because your object does not exists yet. So take care to make the needed checks before referencing the object properties or methods.
请注意,如果您将表单请求用于创建和更新验证,则在创建记录时,业务变量将是null
因为您的对象尚不存在。因此,在引用对象属性或方法之前,请注意进行所需的检查。
回答by pinkal vansia
There can be many ways to achieve this. I do it as below.
可以有很多方法来实现这一点。我这样做如下。
You can have a hidden field 'id' in your business form like bellow,
您可以在您的业务表单中设置一个隐藏字段“id”,如下所示,
{!! Form::hidden('id', $business->id) !!}
and you can retrieve this id
in FormRequest
as below,
你可以id
在FormRequest
下面检索它,
public function rules()
{
$businessId = $this->input('id');
return [
'name' => 'required|unique:businesses,name,'.$businessId,
'url' => 'required|url|unique:businesses'
];
}
回答by FabienChn
For those who switched to laravel 5 :
对于那些切换到 Laravel 5 的人:
public function rules()
{
$business = $this->route('business');
// rest of the code
}
回答by DavidODW
Let say if we have a scenario like we want to change our validation rules depends on the type
that we pass in with the route. For example:
假设我们有一个场景,比如我们想要改变我们的验证规则取决于type
我们通过路由传递的内容。例如:
app.dev/business/{type}
For different type of business, we have different validation rules. All we need to do is type-hint the request on your controller method.
对于不同类型的业务,我们有不同的验证规则。我们需要做的就是对控制器方法的请求进行类型提示。
public function store(StoreBusiness $request)
{
// The incoming request is valid...
}
For the custom form request
对于自定义表单请求
class StoreBussiness extends FormRequest
{
public function rules()
{
$type = $this->route()->parameter('type');
$rules = [];
if ($type === 'a') {
}
return rules;
}
}
回答by godbout
In Laravel 5.5 at least (haven't checked older versions), once you did your explicit binding (https://laravel.com/docs/5.5/routing#route-model-binding), you can get your model directly through $this:
至少在 Laravel 5.5 中(没有检查过旧版本),一旦你做了你的显式绑定(https://laravel.com/docs/5.5/routing#route-model-binding),你可以直接通过 $这个:
class StoreBussiness extends FormRequest
{
public function rules()
{
$rules = [];
if ($this->type === 'a') {
}
return rules;
}
}
回答by Adam
Since Laravel 5.6 you may type hint it in the rules method:
从 Laravel 5.6 开始,你可以在 rules 方法中输入hint:
public function rules(Business $business)
{
return [
'name' => 'required|unique:businesses,name,'.$business->id,
'url' => 'required|url|unique:businesses'
];
}
See the docsfor more:
请参阅文档了解更多信息:
You may type-hint any dependencies you need within the rules method's signature. They will automatically be resolved via the Laravel service container.
您可以在规则方法的签名中键入提示您需要的任何依赖项。它们将通过 Laravel 服务容器自动解析。