php required_if Laravel 5 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37777265/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:22:50  来源:igfitidea点击:

required_if Laravel 5 validation

phpvalidationlaravellaravel-5.2

提问by David

I have form that a user can fill-out for selling their home. And for one of the in puts, a user must select weather it will be "For Sale" or "For Rent". If it is For Sale, two price input fields will appear, and if it is For Rent, then some other price input field will appear based off of jQuery.

我有用户可以填写的表格以出售他们的房屋。对于输入之一,用户必须选择天气,它将是“出售”或“出租”。如果是 For Sale,则会出现两个价格输入字段,如果是 For Rent,则会出现一些基于 jQuery 的其他价格输入字段。

My problem is I want the price fields to be required, BUT for example if I'am selecting "For Rent", and then I submit my form, it will give me an error saying the price fields for the "For Sale" input fields are required, even though it is under the "For Rent" section.

我的问题是我希望价格字段是必需的,但是例如,如果我选择“出租”,然后提交我的表单,它会给我一个错误,说明“待售”输入字段的价格字段是必需的,即使它在“出租”部分下。

I know there is a required_ifin Laravel, but I just dont know how to utilize that. Here is my Requests for a Property.

我知道Laravel 中有一个required_if,但我只是不知道如何利用它。这是我对房产的要求。

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class PropertyRequest 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 [
            'list_type' => 'required',
            'sale_price' => 'required', // <-- maybe like: required_if:value
            'rent_price' => 'required',   
        ];
    }
}

/****************** EDIT ***************************/

/****************** 编辑 ***************************/

What I have now:

我现在所拥有的:

 public function rules()
    {
        return [
            'list_type'  => 'required',
            'sale_price' => 'required_if:list_type:For Sale',
            'rent_price' => 'required_if:list_type:For Rent',
    }

But I get this error when I submit the Form:

但是当我提交表单时出现此错误:

My Error

我的错误

回答by Achraf Khouadja

assuming that list_type is the name of the select box to choose from (values : selling or rent)

假设 list_type 是要从中选择的选择框的名称(值:出售或出租)

use it this way

用这种方式

"sale_price" => "required_if:list_type,==,selling"

what does this mean?:

这是什么意思?

the sale price is only required if the value of list_type is equal to selling

仅当 list_type 的值等于 selling

do the same for rent_price

做同样的事情 rent_price

edit

编辑

public function rules()
{
  return [
   'list_type'  => 'required',
   'sale_price' => 'required_if:list_type,==,For Sale',
   'rent_price' => 'required_if:list_type,==,For Rent'
}

回答by Prafulla Kumar Sahu

There can be another situation when, the requirement will be required if another field is not present, if someone is in this situation, you can do

可能还有另一种情况,如果另一个字段不存在,则需要该要求,如果有人处于这种情况,您可以这样做

'your_field.*' => 'required_unless:dependency_field.*,

回答by Kiran Maniya

You can use Illuminate\Validation\Rulein Laravel as given below to build the validator.

您可以Illuminate\Validation\Rule在 Laravel中使用如下所示来构建验证器。

$validator =  Validator::make( $request->input(), [
    'list_type' => 'required',
    'sale_price'=> Rule::requiredIf( function () use ($request){
        return $request->input('list_type') == 'For Sale';
    }),
    'rent_price'=> Rule::requiredIf( function () use ($request){
        return $request->input('list_type') == 'For Rent';
    }),
]);

回答by Rob

You could also use the Illuminate\Validation\Rules\RequiredIfrule directly.

您也可以Illuminate\Validation\Rules\RequiredIf直接使用该规则。

class SomeRequest extends FormRequest
{
    ...
    public function rules()
    {
        return [
            'sale_price' => new RequiredIf($this->list_type == 'For Sale'),
            'rent_price' => new RequiredIf($this->list_type == 'For Rent'),
        ];
    }
}

And if you need to use multiple rules, then you can pass in an array.

而如果你需要使用多个规则,那么你可以传入一个数组。

public function rules()
{
    return [
        'sale_price' => [
            new RequiredIf($this->list_type == 'For Sale'),
            'numeric', 
            'string',
            ...
        ]
    ];
}