Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spencer's avatar

Multiple Validation Rules of the Same Type

I know that multiple validation rules of the same type work, but they all return the same error to the user.

I have the following validation rules on a 'domain' field:

'domain' => [
        'required',
        'string',
        'regex:~^[^@]+$~',
        'regex:~^[a-zA-Z0-9\.-]+(\.[a-zA-Z]+)+$~',
        'regex:~^(?!mail\.).+~',
        //unique: table, column, exception, exceptionValue, where, whereValue
        'unique:domain,name,NULL,id,status,pending',
        'unique:domain,name,NULL,id,status,active',
        'unique:domain,name,NULL,id,status,suspended',
        'unique:alias,name,NULL,id,status,active',
        'unique:order_block,criteria',
      ],

For each of the following regex rules I'd like different errors for the user to read. I don't have the wording down yet. But I won't need to if I can't do it anyways. But if I could it would be something like this:

 'regex:~^[^@]+$~',

This is a domain wide service not specific to a single email address and should not have

 'regex:~^[a-zA-Z0-9\.-]+(\.[a-zA-Z]+)+$~',

The domain name seems to be formatted incorrectly.

'regex:~^(?!mail\.).+~',

The Domain Name you entered looks like your mail server address.

Just to reiterate, I am asking if and how can I customize multiple error messages for multiple validation rules of the same type on the same field?

0 likes
7 replies
SaeedPrez's avatar

I'm haven't tried this but I'm thinking either you need to do your own validation or you could split the rules into groups and run multiple validations so that you can give them custome messages..

// First validation with custom error messages
$messages = [
    'domain.required' => 'I pitty the fool who does not fill out the domain field',
    'domain.string' => 'I also pitty stupid fools',
    'domain.regex' => 'I might have to come kick your ass',
    'domain.unique' => 'Yeah, I am coming for your ass!',
];

'domain' => [
        'required',
        'string',
        'regex:~^[^@]+$~',
        'unique:domain,name,NULL,id,status,suspended',
]

// Second validation with custom error messages
$messages = [
    'domain.regex' => 'How hard is it.. ABCDEF...',
    'domain.unique' => 'bleh',
];

'domain' => [
        'regex:~^[a-zA-Z0-9\.-]+(\.[a-zA-Z]+)+$~',
        'unique:domain,name,NULL,id,status,active',
]

// And so on... you get the point..
$messages = [
    'domain.regex' => 'The Domain Name you entered looks like your mail server address.',
    'domain.unique' => 'U.N.I.Q.U.E .... Gaaaaaaaaah!',
];

'domain' => [
        'regex:~^(?!mail\.).+~',
        'unique:domain,name,NULL,id,status,pending',
]

'domain' => [
        'unique:alias,name,NULL,id,status,active',
]

'domain' => [
        'unique:order_block,criteria',
]

Or maybe make a loop..

$customValidations = [
    'regex:~^(?!mail\.).+~' => 'The Domain Name you entered looks like your mail server address.',
    'regex:~^[a-zA-Z0-9\.-]+(\.[a-zA-Z]+)+$~' => 'Some other error message',
];

foreach($customValidations as $rule => $msg) {

    $validator = Validator::make();

    if($validator->fails()) {
        return $msg;
    }

}

Anyways, that's ll I got for 2 am.. good night and good luck.

Spencer's avatar

I would prefer to use the premade validation rules if at all possible... So running multiple validations seems like a good option. How would I go about making the Laravel run multiple validations, if I want to still use type hinting to get my validated request.

public function store( CreateOrderRequest $request )
  {
      // store $request as order
  }

Would I use a type hint in my CreateOrderRequest? Would that even work? If it works is that the proper way to do multiple validations.... is doing multiple validations a good idea?

Would I validate before this validation runs using type hinting like this?

public function rules(CreateOrderRequestPreValidation $request)
  {

    $this->formatInput();

    $rules = [
      'domain' => [
        'required',
        'string',
        'regex:~^[^@]+$~',
        'regex:~^[a-zA-Z0-9\.-]+(\.[a-zA-Z]+)+$~',
        'regex:~^(?!mail\.).+~',
        //unique: table, column, exception, exceptionValue, where, whereValue
        'unique:domain,name,NULL,id,status,pending',
        'unique:domain,name,NULL,id,status,active',
        'unique:domain,name,NULL,id,status,suspended',
        'unique:alias,name,NULL,id,status,active',
        'unique:order_block,criteria',
      ],

    //   ... lots of other rules

    ];


    return $rules;
  }
public function formatInput()
  {
    $input = $this->all();

    $input[ 'domain' ] = preg_replace( '~^http[s]*://~', '', $input[ 'domain' ] );
    $input[ 'domain' ] = preg_replace( '~^www\.~', '', $input[ 'domain' ] );

    // ... other fields being formated

    $this->replace( $input );
  }

Sorry I have so many questions. I'm just new to Laravel. The framework is so well organized, I'd hate to ruin my project by using coding practices that aren't proper and predictable.

SaeedPrez's avatar
Level 50

Don't be so hard on yourself,.. Laravel is just a tool and it can be used in many different ways, if you're not happy with something, you can always come back and refactor it later.

I would probably do something like this... Let's assume you're updating a model..

public function update(Request $request, $id)
{
    $request = $this->formatInput($request);

    for($i = 1; $i <= 5; $i++) {
        $validator = $this->validateInput($request, $i);

        if($validator->fails()) {
            return back();
        }
    }

    // Update code
}

private function validateInput($request, $i)
{
    // Setup your rules and messages
    $rules[1] = [];
    $messages[1] = [];

    $rules[2] = [];
    $messages[2] = [];

    return Validator::make($input, $rules[$i], $messages[$i]);
}

private function formatInput($request)
{
    // format input code
    return $request;
}

This is just an example to give you an idea, it would be even a better idea if you moved the private functions to your model or create a separate class for them.

1 like
pmall's avatar

Just to reiterate, I am asking if and how can I customize multiple error messages for multiple validation rules of the same type on the same field?

You should use a custom validator, create a custom rules for all your rules above, and assign error messages to those custom rules.

SaeedPrez's avatar

@pmall is it possible to create custom messages when you have multiple 'domain.regex' and 'domain.unique'?

pmall's avatar

I dont know but for example if the three regex above are wrapped in custom rules, those custom rules would have different names so problem solved.

1 like
Spencer's avatar

The reason I'd rather use the Laravel validation than my own custom one is because the more I rely on Laravel the more my code benefits from Laravel updates that are backward compatible. Plus I'm not super familiar with regex, so if I can use a rule that I know will work rather than write my own with my limited understanding of regex, I have to go with that.

Please or to participate in this conversation.