2

In request I got attribute that defines validation rules and flow. Let's say it is account_type: business, personal. Each value changes validation flow and requires different attributes to be present ir request.

Let's say I have following custom rules methods:

public function validateAccountTypeBusiness($attribute, $value, $parameters, Validator $validator)
{
   // check is present:
   // Company address
   // VAT number
}

public function validateAccountTypePersonal($attribute, $value, $parameters, Validator $validator)
{
   // check is present:
   // User mobile phone
}

Since each rule requires more than single attribute to be presented in this request depending on account type ( vat number, company name, etc ) returning false would be not informative to user because generated response notices that account type is invalid without any details which actual attributes is missing.

The question is: how I can append more rules to be validated in this validation custom rule?

Following not working:

public function validateAccountTypeBusiness($attribute, $value, $parameters, Validator $validator)
{
    $validator->addRules([
        'company_address'   =>  'required|string',
        'vat_number'        =>  'required',
    ]);
}
1

2 Answers 2

5

If you follow Complex Conditional Validation, you can validate using the following way

Step 1: Create Validator instance with static rules which would be same in both the cases(business or personal account) like name,check,phone,email,etc.

$v = Validator::make($data, [
    'email' => 'required|email',
    'name' => 'required|alpha',
    'phone' => 'required|numeric',
    'check' => 'required' //common in both account type
    //add more rules as required
]);

Step 2: Add specific rules with condition

$v->sometimes('VAT', 'required|max:50', function ($input) {
    return $input->account_type == 'business';
});

$v->sometimes('Company_address', 'required|max:500|alpha', function ($input) {
    return $input->account_type == 'business';
});

$v->sometimes('user_mobile', 'required|numeric', function ($input) {
    return $input->account_type == 'personal';
});

Parameters for sometimes() method :

The first argument passed to the sometimes method is the name of the field we are conditionally validating. The second argument is the rules we want to add. If the Closure passed as the third argument returns true, the rules will be added.

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

Comments

0

As per my understanding, Custom validation rule can be used for only one parameter & you're trying to club them together.

This is one of many ways to approach your problem. Hope it helps.

<?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AccountController extends Controller
{
    /**
     * Store a new user account.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if($request->input('account_type' == 'business') {
          // Validation for business account
            $validator = Validator::make($request->all(), [
                'company_address' => 'required|string',
                'vat_number' => 'required',
            ]);
        }
        else {
         // Validation for personal account
            $validator = Validator::make($request->all(), [
                'contact_number' => 'required',
            ]);
        }

        if ($validator->fails()) {
            return redirect('account/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // Store the user account...
    }
}

Reference -

Manually creating validators - https://laravel.com/docs/5.4/validation#manually-creating-validators

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.