我有一个用户可以填写的表格来出售他们的房子.对于其中一个输入,用户必须 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',
}
但当我提交表格时,我发现了这个错误: