Complex Conditional Validation - Not working :(

Posted 1 year ago by almost_pitt

Hey guys,

I'm trying to use complex conditional validation for my form. In it, I would like for the field Furnished_2 to be required if Number_of_Bedrooms is set to 2 or more and Renting_Entire_Property is set to Renting_Entire_Property.

Here is my first attempt using required_if.

'Furnished_2' => 'required_if:Renting_Entire_Property,Renting_Entire_Property|required_if:Number_of_Bedrooms,>=,2',

Apparently, you can only use 1 required_if statement at a time. So, this didn't work.

Then, I tried Complex Conditional Validation as outlined here: https://laravel.com/docs/5.4/validation, although I am a bit uncertain of how it actually works.

Here is how my validation works:

    public function store(Request $request)
    {
        $this->validate(request(), [
            'Title' => 'required|max:45|min:10',
            'Type_of_Property' => 'required',
            'Floor_Level' => 'required',
            'Elevator' => 'required',
            'Number_of_Bedrooms' => 'required',
            'Number_of_Bathrooms' => 'required',
            'Furnished' => 'required',

            'Number_of_Beds_1' => 'required',
            'Type_of_Beds_1_1' => 'required',
            'Furnished_1' => 'required',
            'Number_of_Guests_1' => 'required',

            'Security_Deposit' => 'required_if:Security_Deposit_Check,==,Security_Deposit_Check',

            'Landlords_Comments_1' => 'max:280',
            'Landlords_Comments_2' => 'max:280',

            'formatted_address' => 'required|min:10',
            'locality' => 'required',

            'price' => 'required|integer|between:100,9999',
            'datepicker' => 'required',
            'Minimum_Rental_Period' => 'required',
        ]);

Then, I'm not really sure how to insert the validator. I tried to insert it after this. Such as:

        $v = Validator::make($data, [
        ...static validation here...
        ]);

        $v->sometimes('Furnished_2', 'required', function ($input) {
            return $input->Number_of_Bedrooms >= 2;
            return $input->Renting_Entire_Property >= 'Renting_Entire_Property';
        });

and I inserted use Validator; at the top. Unfortunately, I keep running into errors.

Here is the error that I recieve:

(1/1) ErrorException

Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given, called in /home/stude711/studenthousing/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 221 and defined

Any idea how to complete the Complex Conditional Validation?

Thanks!

Please sign in or create an account to participate in this conversation.