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

nunomira's avatar

validate more than one field simultaneously

Using Form Request Validation (preferably), is there a way to validate more than one field simultansously?

For example, suppose you have two fields: firstname and lastname, each with their own validation rules. Suppose that each have a max chars rule of 20 (max:20) But, you don't want firstname and lastname combined to have more than 30 chars. How would you go about this?

0 likes
11 replies
skliche's avatar

Not sure if there is something out of the box for that, but you could create your own rule, use a closure, or use sometimes, see Complex Conditional Validation. You can access other fields using the request() helper, sometimes already provides access to other values through the parameter input.

nunomira's avatar

Thanks @skliche I don't think this logic would work in my case. I believe the field has to exist (e.g.: fullname)

As I'm using ajax, I'll create an extra field dynamically

fullname = firstname + " " + lastname;

and I'll validate it.

Even if I weren't using ajax, I could do it with JavaScript alone, using a hidden input. I just would like to do it without depending on JavaScript.

skliche's avatar

The field has to exist in the data that is passed to the validator, that's all. It can come from a request's payload, be made up on the fly, whatever ... Is fullname included in the request? Then you are fine. It doesn't have to exist in any model or database if that's what you are concerned about. I'm testing my custom validators with arbitrary fields all the time.

nunomira's avatar

The field existing in the request is my only concern. What I'm doing now is something like:

data.append("fullname", firstname + " " + lastname);

in my ajax request.

I found this to be a good solution in my case.

My question now is: Am I able to do something like this on the server side (before the validation takes place) so I can do it with php only?

Something like

$request->input->fullname = $request->input->firstname . " " . $request->input->lastname

If something like this were possible, it would be great.

nunomira's avatar

Actually I've been able to do it using the input function in the Form Request Validation Class!

Something like this :

public function input($key = NULL, $default = NULL)
{
    $attributes = parent::input();
    $attributes['fullname'] = $attributes['firstname'] . ' ' . $attributes['lastname'];
    return $attributes;
}
skliche's avatar

If you do data.append("fullname", firstname + " " + lastname); and then submit that data on the Javascript side, fullname is already in the request's data. No need to do it on the PHP side again.

rawilk's avatar

To me, that just seems unnecessary. It's really easy to just create a custom rule that can do that validation, plus if you need to do the same kind of validation on another request, you don't have to duplicate logic now.

1 like
nunomira's avatar

@skliche sure! Like I said, I just prefere it on the server side :)

@wilk_randall that's what I was after in the first place, but wasn't able to do. Feel free to help! It will be appreciated!

skliche's avatar
skliche
Best Answer
Level 42

We are running around in circles ... Did you even try what I've already proposed?

For example in the rules() method of your form request:

return [
    'firstname' => [function($attribute, $value, $fail) {
        if (request()->has('lastname') && ! empty(request('lastname'))) {
            // do your validation here when both fields are present, e.g. both strings are max 30 chars
        } else {
            // do your validation here when just the first name is present, e.g. string is max 20 chars
        }
    }],
    'lastname' => 'max:20',
];

See https://laravel.com/docs/master/validation#using-closures

And, sure, if you need that more often, you could create your own validation rule.

nunomira's avatar

No. I didn't understand how I would be able to apply the rule with the closure to more than one field. Now it became clear. Thank you very much!

skliche's avatar

No problem, just let us know when there is something you don't understand or it is not clear to you how to apply something. It's hard to know what's going on in your mind or what exactly is causing trouble :-)

Please or to participate in this conversation.