当其他三个字段为空时,需要字段

时间:2016-11-01 12:34:09

标签: laravel

我有4个输入字段。其中一个必须填补。

我的领域:

<input name="name" placeholder="Name">
<input name="hair_style" placeholder="Style">
<input name="hair_color" placeholder="Color">
<input name="options" placeholder="Options">

我的功能

$this->validate($request, [
    'name' => 'required_if:hair_style,0,',
]);

因此当hair_style为0时,必须填写输入字段名称。这可行,但..我想在下面这样,但我不知道如何:

$this->validate($request, [
    'name' => 'required_if:hair_style,empty AND hair_color,empty AND options,empty,',
]);

它必须像这样工作。当hair_stylehair_coloroptions为空时,name必须填写。但这可能是required_if吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试:

'name' => 'required_if:hair_style,0|required_if:hair_color,0||required_if:options,0',

<强>更新

您可以conditionally add rules作为:

$v = Validator::make($data, [
   'name' => 'min:1',
]);


$v->sometimes('name', 'required', function($input) {
   return ($input->hair_style == 0 && $input->hair_color == 0 && $input->options == 0);
});

如果您需要,可以在闭包中添加更多逻辑...例如empty检查。

答案 1 :(得分:0)

所以我所要做的就是:

this.DeleteProd = function (prodId) {
    var response = $http({
        method: "DELETE",
        url: "https://apirepo.leofaj.org/products/RemProduct", 
         params: {
             prodId: prodId
         },
      //  data:prodId,
        //dataType:"json"
    });
    return response;
}

了解更多信息,请查看https://laravel.com/docs/5.3/validation#rule-required-without-all

相关问题