简体   繁体   English

required_if Laravel 5 验证

[英]required_if Laravel 5 validation

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_if in Laravel, but I just dont know how to utilize that.我知道 Laravel 中有一个required_if ,但我只是不知道如何利用它。 Here is my Requests for a Property.这是我对房产的要求。

<?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:但是当我提交表单时出现此错误:

我的错误

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_pricerent_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'
}

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

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

You could use the Illuminate\\Validation\\Rules\\RequiredIf rule directly.您可以直接使用Illuminate\\Validation\\Rules\\RequiredIf规则。

Note: This rule is available in Laravel 5.6 and up.注意:此规则在 Laravel 5.6 及更高版本中可用。

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'),
            'string',
            ...
        ]
    ];
}

You can use Illuminate\\Validation\\Rule in Laravel as given below to build the validator.您可以使用 Laravel 中的Illuminate\\Validation\\Rule来构建验证器,如下所示。

$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';
    }),
]);

For me, with this required_if i needed to check with two values and I wrote as对我来说,有了这个required_if我需要检查两个值,我写为

return [
        'change_status' => 'required',
        'forward_to' => 'required_if:change_status,2,3'
    ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM