required_if Laravel 5 validation

PhpValidationLaravelLaravel 5.2

Php Problem Overview


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.

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

Php Solutions


Solution 1 - Php

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? :

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

do the same for 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'
}

Solution 2 - Php

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.*,

Solution 3 - Php

You could use the Illuminate\Validation\Rules\RequiredIf rule directly.

Note: This rule is available in Laravel 5.6 and up.

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

Solution 4 - Php

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

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

Solution 5 - Php

For me, with this required_if i needed to check with two values and I wrote as

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - PhpAchraf KhouadjaView Answer on Stackoverflow
Solution 2 - PhpPrafulla Kumar SahuView Answer on Stackoverflow
Solution 3 - PhpRobView Answer on Stackoverflow
Solution 4 - PhpKiran ManiyaView Answer on Stackoverflow
Solution 5 - Phpsh6210View Answer on Stackoverflow