1

My controller like this :

<?php
use App\Http\Requests\StoreReceiveOrderRequest;
class SellController extends Controller
{
    public function receiveOrder(StoreReceiveOrderRequest $request)
    {
        dd($request->all());
        ...
    }
}

Before executed statement in the receiveOrder method, it will check rules on the StoreReceiveOrderRequest

The StoreReceiveOrderRequest like this :

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreReceiveOrderRequest extends FormRequest
{
    public function rules()
    {
        return [
            'is_follow_up'=>'required',
            'note'=>'max:300' // I want to make this to be required if is_follow_up = n
        ];
    }
}

the result of dd($request->all());, there are 2 results, depending user input

If the is_follow_up = y, the result like this :

Array
(
    [is_follow_up] => y
)

If the is_follow_up = n, the result like this :

Array
(
    [is_follow_up] => n
    [note] => test
)

If is_follow_up = n, I want to make the note is required

If is_follow_up = y, the note is not required

Seems it must to add condition on the rules

How can I do it?

4 Answers 4

3

There is a validation rule that does exactly this already. The Laravel docs for validation list all the available rules.

'note' => 'required_if:is_follow_up,n|...'

Laravel 5.3 - Docs - Validation - Rule - required if

Sign up to request clarification or add additional context in comments.

1 Comment

Seems you can help me. Look at this. stackoverflow.com/questions/47726407/…. This is a bit different
2

Just change your validation to the following-

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
    class StoreReceiveOrderRequest extends FormRequest
    {
       public function rules()
       {
         $rules = ['is_follow_up'=>'required',
           ];

         if (Input::get('is_follow_up')=='n') {
          $rules += [
             'note'=>'max:300'
          ];
         }
       }
    }

Comments

0

You can also write your conditions in Laravel Request File

public function rules()
    {
        $rules = [
            'name' => ['required', 'string', 'max:255'],
            'order' => ['nullable', 'integer'],
            'type' => ['required', 'in:category,brand,vendor,image'],
            'category_id' => ['required_if:type,category'],
            'brand_id' => ['required_if:type,brand'],
            'vendor_id' => ['required_if:type,vendor'],
            'image_size' => ['required_if:type,image','in:small,medium,large'],
            'images.*' => ['required_if:type,image'],
            
            'image_links' => ['required_if:type,image,url'],
        ];

        if (request()->get('image_size') == 'large') {
            $rules += [
                'images' => 'max:1',
            ];
        } elseif (request()->get('image_size') == 'medium') {
            $rules += [
                'images' => 'min:2|max:2',
            ];
        } elseif (request()->get('image_size') == 'small') {
            $rules += [
                'images' => 'min:3|max:3',
            ];
        }

        return $rules;
    }

Comments

-1

Read The Laravel docs for list of all the available validation rules.

'note' => 'required_if:is_follow_up,n|...'

4 Comments

Seems you can help me. Look at this. stackoverflow.com/questions/47726407/…. This is a bit different
@akramwahld i really dont understand what is the point of posting the same answer that is already here ... 2 days later
@lagbox ,its none of your business, i can answer any question at anytime I can, and its very apparent am not violating SO rules,
i wasn't implying there was a violation of rules, it is none of my business, sorry for the confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.