required_if Laravel 5 验证 [英] required_if Laravel 5 validation

查看:42
本文介绍了required_if Laravel 5 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

<?php

namespace AppHttpRequests;

use AppHttpRequestsRequest;

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 ***************************/

我现在拥有的:

 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:

推荐答案

假设 list_type 是可供选择的选择框的名称(值:出售或出租)

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

这样使用

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

这是什么意思?:

仅当list_type的值等于 sell

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

rent_price

编辑

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

这篇关于required_if Laravel 5 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆