0

I am beginner in Laravel. I need validation to my project. I use Laravel 8. I have this code in TaskRequest:

public function rules()
    {
        return [
            'speed_number' => ['required', 'string', 'min:3', 'max:255'],
            'order_number' => ['required', 'string', 'min:3', 'max:255'],
            'address_from' => ['required', 'string', 'min:3', 'max:255'],
            'address_to' => ['required', 'string', 'min:3', 'max:255'],

            'carrier_name' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_nip' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_street' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_email' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_phone' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_postal_code' => ['required', 'string', 'min:3', 'max:255'],
            'carrier_city' => ['required', 'string', 'min:3', 'max:255'],
        ];
    }

I need something like this:

public function rules()
        {
            return [
                'speed_number' => ['required', 'string', 'min:3', 'max:255'],
                'order_number' => ['required', 'string', 'min:3', 'max:255'],
                'address_from' => ['required', 'string', 'min:3', 'max:255'],
                'address_to' => ['required', 'string', 'min:3', 'max:255'],
    
if($company == true){


                'carrier_name' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_nip' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_street' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_email' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_phone' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_postal_code' => ['required', 'string', 'min:3', 'max:255'],
                'carrier_city' => ['required', 'string', 'min:3', 'max:255'],
}


            ];
        }

(I need add if statement in request validation code). How can I make it?

Please help me :)

4 Answers 4

3

Seems like you're looking for the required_if rule. See the docs: https://laravel.com/docs/9.x/validation#rule-required-if

public function rules()
{
    return [
        'speed_number' => ['required', 'string', 'min:3', 'max:255'],
        // ... and other rules ...
        'carrier_name' => ['required_if:company,1', 'string', 'min:3', 'max:255'],
        // ... and other rules ...
    ];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Isn't this only applicable when company is also in the request? Which it doesn't seem to be in the example provided?
I would assume that's what OP meant.
I have parametr in my request: transport_type. If this == 1 then I need additional validation: 'carrier_name' => ['required', 'string', 'min:3', 'max:255'], 'carrier_nip' => ['required', 'string', 'min:3', 'max:255'], 'carrier_street' => ['required', 'string', 'min:3', 'max:255'], 'carrier_email' => ['required', 'string', 'min:3', 'max:255'], 'carrier_phone' => ['required', 'string', 'min:3', 'max:255'], 'carrier_postal_code' => ['required', 'string', 'min:3', 'max:255']...
@jkloip - Exactly. For all of those attributes, use required_if:transport_type,1 instead of simply required.
1

You need use the Rule::requiredIf

use Illuminate\Validation\Rule;

'carrier_name' => [
    Rule::requiredif(function () use ($company) {
        return $company == true ? true : false;
    }),
    'string', 'min:3', 'max:255'
],

Comments

0

Since from the question it's not clear if company is in the request.

If it is not you could just return different values based on $company

public function rules() {
 if ($company) {
  // return rules for when company exists
 } else {
  // return rules for when company does not exist
 }
}

If company is in the request, you should take a look at the required_if validation Laravel provides. Laravel Docs - Required if

Comments

0

A more efficient way to do it would be this one:

        $rules =  [
            'speed_number' => ['required', 'string', 'min:3', 'max:255'],
            'order_number' => ['required', 'string', 'min:3', 'max:255'],
            'address_from' => ['required', 'string', 'min:3', 'max:255'],
            'address_to' => ['required', 'string', 'min:3', 'max:255'],
            'data_start' => ['required', 'string', 'min:3', 'max:60'],
            'data_finish' => ['required', 'string', 'min:3', 'max:60'],
            'transport_type'  => 'required'
        ] + ($request->carrier)->map(function ($attr, $key){
            return [
                "carrier.$key" => [Rule::requiredIf($request->transport_type == 2),'min:3', 'max:255'],
            ];
        })->toArray();

        Validator::make($request->all(),  $rules)->validate();

Comments

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.