我有一个用户可以填写的表格来出售他们的房子.对于其中一个输入,用户必须 Select 是"出售"还是"出租".如果是出售的,则会出现两个价格输入字段;如果是出租的,则会根据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.

我知道拉维尔有一个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',
    }

但当我提交表格时,我发现了这个错误:

My Error

推荐答案

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

use it this way

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

what does this mean? :

仅当列表类型的值等于selling时,才需要销售价格

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

Laravel相关问答推荐

Livewire 3分页在点击后不更新页面

为什么使用 created_at 表在迁移错误中创建表?

如何在 Laravel 9 中获取带分页的订单列表?

如何提取每组 groupBy 查询的唯一值?

如何防止 Laravel 路由被直接访问(即非 ajax 请求)

使用命令行界面停止 laravel 服务器

Laravel & Docker:无法打开流或文件/var/www/html/storage/logs/laravel.log:无法打开流:权限被拒绝

为什么 Laravel 5 会自动更新我的日期字段?

Laravel 5.4 中的自定义助手类

Lumen和 MongoDB?

带有 Laravel Passport 的 SSO

Laravel 中间件 except除外规则不起作用

Laravel 随机排序

Laravel 同步错误

Laravel Eloquent 在子查询中有两个WHERE NOT IN

Laravel assets资源与混合

Laravel 的 toSql() 方法是否会屏蔽 id? (列值被问号替换)

在 Laravel 中结合 AND/OR Eloquent的查询

如何在 Laravel 中 Refresh刷新用户对象?

Laravel 5.2 |测试 UploadedFile 在发布后错过了 $test 值?